jsp的登入校正Demo
part_1:login.jsp:登入頁面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <% String fdbkMsg = (String) request.getAttribute("fdbkMsg"); if (null == fdbkMsg) { fdbkMsg = ""; } %> <% Boolean logedIn = (Boolean) session.getAttribute("logedIn"); if (null == logedIn) { logedIn = false; } else if (logedIn) { //如果在本次會話已經登陸,直接重新導向到success-page-1 response .sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp"); } %> <% String username = ""; Cookie[] cookies = request.getCookies(); if ((null != cookies) && (cookies.length > 0)) { for (Cookie c : cookies) { if ("admin".equals(c.getValue())) { username = "admin"; break; } } }//end if-condition %> <body> <br> <div align="center"> 請登入: <br> <form action="/ServletDemoProject/servlet/LoginVerificationServlet" method="post"> 使用者名稱: <input type="text" name="username" value="<%=username%>" /> <br> 密 碼: <input type="password" name="password" value="" /> <br> <font color='red'><%=fdbkMsg%></font> <br> <input type="submit" value="提交" /> <br> </form> </div> </body> </html>
part_2:LoginVerificationServlet.java:校正登入資訊,此處沒有串連資料庫,預設只有username:admin,password:888888才算登入成功;登陸失敗時:重新轉寄到Login.jsp並提示使用者登陸失敗,重新登陸;
package cn.mike.servlet.test_1209_Login; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginVerificationServlet extends HttpServlet { private static final long serialVersionUID = -6886327892796230543L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if (("admin".equals(username)) && ("888888".equals(password))) {// 登入成功 // 儲存cookie到用戶端 Cookie userCookie = new Cookie("username", username); userCookie.setMaxAge(60 * 2);// expiry : 2 minutes response.addCookie(userCookie); // 重新導向到一個新的頁面,並提示XXX使用者登入成功(使用session存取使用者名稱); request.getSession().setAttribute("username", username); request.getSession().setAttribute("logedIn", true); response .sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp"); } else {// 登陸失敗 // 轉寄到登入介面,並提示錯誤資訊: request.setAttribute("fdbkMsg", "使用者名稱或密碼錯誤!"); request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // do same as GET-method : doGet(request, response); } }
part_3:success-page-1.jsp:校正登入成功後重新導向到該頁面,提示使用者已經成功登陸;如果使用者試圖通過不正當途徑,e.g:從地址欄訪問,將會轉寄到登入介面,並作提示;
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success-page-1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <% String username = (String) session.getAttribute("username"); if (null == username) { //如果username為空白值,說明不是通過正常渠道來的,轉寄到Login頁面; request.setAttribute("fdbkMsg", "別想走後門進來,趕緊登入!"); request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( request, response); } %> <body> <br> <%=username%>已經成功登陸。 <br> <font>您可以選擇瀏覽:</font> <br> <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">點這兒有精彩.</a> <br> <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">點這兒更精彩.</a> <br /> <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">你敢點這兒嗎.</a> <br /> </body> </html>
part_4:success-page-2.jsp:登陸成功頁面2,如果已經登陸成功將使用者名稱儲存到session,在訪問該頁面時將會校正一下,防止從地址欄暴力訪問;
<%@ page language="java" import="java.util.Date" pageEncoding="UTF-8"%> <%@ page language="java" import="java.text.SimpleDateFormat"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success-page-2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <% String username = (String) session.getAttribute("username"); if (null == username) { request.setAttribute("fdbkMsg", "呵呵嗒,這裡是你來的地方嗎?快登陸!"); //轉寄到登入介面: request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( request, response); } SimpleDateFormat sDateFormat = new SimpleDateFormat("a"); Date today = new Date(); String am_pm_str = sDateFormat.format(today); String am_pm_str_in_chinese = ""; if ("am".equalsIgnoreCase(am_pm_str)) { am_pm_str_in_chinese = "上午"; } else am_pm_str_in_chinese = "下午"; // set null; sDateFormat = null; today = null; am_pm_str = null; %> <body> <br /> <font><b><%=username%><%=am_pm_str_in_chinese%>好,能來到頁面2真不簡單.</b> </font> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。