業務描述
用戶在login.jsp頁面輸入用戶名密碼登錄:
如果用戶名為xingoo,密碼為123,則跳轉到成功界面login_success.jsp,並顯示用戶登錄的名字;
如果用戶名密碼錯誤,則跳轉到失敗界面login_failure.jsp,並提示返回登錄界面。
login.jsp代碼
<%@ 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>用戶登錄</title> </head> <body> <h1>用戶登錄</h1> <hr> <form name="regForm" action="doLogin.jsp" method="post"> <table> <tr> <td>username</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>password</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="submit"/></td> </tr> </table> </form> </body> </html>
dologin.jsp處理代碼
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String username = ""; String password = ""; request.setCharacterEncoding("utf-8"); username = request.getParameter("username"); password = request.getParameter("password"); if("xingoo".equals(username)&&"123".equals(password)){ session.setAttribute("loginUser",username); request.getRequestDispatcher("login_success.jsp").forward(request,response); }else{ response.sendRedirect("login_failure.jsp"); } %>
login_success.jsp用戶登錄成功界面
<%@ 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>用戶登錄</title> </head> <body> <h1>用戶登錄</h1> <hr> 歡迎您!<%=session.getAttribute("loginUser") %> </body> </html>
login_failure.jsp用戶登錄失敗界面
<%@ 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>用戶登錄</title> </head> <body> <h1>用戶登錄</h1> <hr> 登錄失敗!<a href="login.jsp">返回登錄</a> </body> </html>