码迷,mamicode.com
首页 > 其他好文 > 详细

登录注册 servlet

时间:2017-08-21 21:07:34      阅读:510      评论:0      收藏:0      [点我收藏+]

标签:val   redirect   inpu   ring   getattr   存在   uid   rect   let   

package com.hanqi;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");
        String realname = request.getParameter("realname");
        
        System.out.println("姓名:"+realname);
        
        if(checkParam(username,password,password1)){
            if(password.equals(password1)){
                Object obj = request.getServletContext().getAttribute(username);
                if(obj==null){
                    request.getServletContext().setAttribute(username,username);
                    response.sendRedirect("massage.jsp?code=1");
                }else{
                    response.sendRedirect("massage.jsp?code=4");
                }
            }else{
                response.sendRedirect("massage.jsp?code=3");
            }    
        }else{
            response.sendRedirect("massage.jsp?code=2");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
    
    public boolean checkParam(String...args){
        for(String s:args){
            if("".equals(s)||s==null){
                return false;
            }
            
            
        }
        return true;
        
    }

}
package com.hanqi;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        ServletContext application = request.getServletContext();
        Object obj = application.getAttribute(username);
        if(obj!=null) {
            String s_username = (String)obj;
            if(username.equals(s_username)) {
                response.sendRedirect("index.jsp");
            } else {
                response.sendRedirect("message.jsp?code=5");
            }
        } else {
            response.sendRedirect("message.jsp?code=6");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="LoginServlet" method="post">
    username:<input type="text" name="username" />
    password:<input type="text" name="password" />
    <input type="submit" value="登录" />

</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="refresh" content="3;url=index.jsp">
<title>Insert title here</title>
</head>
<body>
<% 
String code = request.getParameter("code");
if("1".equals(code)){
    out.print("<h1>注册成功!</h1>");
}
if("2".equals(code)){
    out.print("<h1>请将信息输入完整 !</h1>");
}
if("3".equals(code)){
    out.print("<h1>两次输入的密码不一致 !</h1>");
}if("4".equals(code)){
    out.print("<h1>用户名已经存在  !</h1>");
}if("5".equals(code)){
    out.print("<h1>用户名不正确  !</h1>");
}
if("6".equals(code)){
    out.print("<h1>用户名不存在 !</h1>");
}



%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="RegisterServlet" method="post">
    username:<input type="text" name="username" /><br>
    password:<input type="text" name="password" /><br>
    password1:<input type="text" name="password1" /><br>
    realname:<input type="text" name="realname" /><br>
    <input type="submit" value="提交" />
    
</form>
</body>
</html>

 

登录注册 servlet

标签:val   redirect   inpu   ring   getattr   存在   uid   rect   let   

原文地址:http://www.cnblogs.com/jgjk/p/7406593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!