實現步驟: 1、創建用戶登錄提交界面 2、創建處理用戶登錄請求servlet組件Main 3、創建代表登錄成功響應的servlet的組件LoginSuccess 4、創建代表登錄失敗響應的servlet組件LoginFail 【1代碼login.html】 【2程式Main.java】 【3程式Lo ...
實現步驟:
1、創建用戶登錄提交界面
2、創建處理用戶登錄請求servlet組件Main
3、創建代表登錄成功響應的servlet的組件LoginSuccess
4、創建代表登錄失敗響應的servlet組件LoginFail
【1代碼login.html】
<!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>登錄</title> </head> <body bgcolor="#FFFFFF"> <center>歡迎登錄系統</center> <form name="login" action="Main" method="post"> <label>用戶名:</label> <input type="text" name="userID" value=""> <label>密碼:</label> <input type="password" name="password" value=""> <input type="submit" name="tj" value="提交"></input> <input type="reset" name="reset"></input> </form> </body> </html>
【2程式Main.java】
package example.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; 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 Main */ @WebServlet("/Main") public class Main extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uerID=request.getParameter("userID"); if (uerID==null) uerID=""; String password=request.getParameter("password"); if(password==null) password=""; if(uerID.equals("guest")&&password.equals("guest")){ RequestDispatcher dispatcher=request.getRequestDispatcher("LoginSuccess"); dispatcher.forward(request, response); }else { RequestDispatcher dispatcher=request.getRequestDispatcher("LoginFail"); dispatcher.forward(request, response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } public String getServletinfo(){ return "short description"; } }
【3程式LoginSuccess.java】
package example.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/LoginSuccess") public class LoginSuccess extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); PrintWriter out=response.getWriter(); String name=request.getParameter("userID"); out.println("<html>"); out.println("<head>"); out.println("<title>登錄成功</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>歡迎"+name+"您已成功登錄系統</h1>"); out.println("</body>"); out.println("</html>"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } public String getServletInfo(){ return "short description"; } }
【4程式LoginFail.java】
package example.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; 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 LoginFail */ @WebServlet("/LoginFail") public class LoginFail extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); PrintWriter out=response.getWriter(); out.print("<html>"); out.print("<head>"); out.print("<title>登錄失敗</title>"); out.print("</head>"); out.print("<body>"); out.print("<h1>登錄失敗,請重新登錄</h1>"); RequestDispatcher dispatcher=request.getRequestDispatcher("login.html"); dispatcher.include(request, response); out.print("</body>"); out.print("<html>"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } public String getServletInfo(){ return "short description"; } }