1. 程式人生 > >登錄註冊 servlet

登錄註冊 servlet

-s user index font password action itl ole eth

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