jsp實現使用者自動登入功能
阿新 • • 發佈:2020-04-22
理解並掌握Cookie的作用以及利用cookie實現使用者的自動登入功能,實現下圖效果
當伺服器判斷出該使用者是首次登入的時候,會自動跳轉到登入介面等待使用者登入,並填入相關資訊。通過設定Cookie的有效期限來儲存使用者的資訊,關閉瀏覽器後,驗證是否能夠自動登入,若能登入,則列印歡迎資訊;否則跳轉到登入頁面。
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%request.setCharacterEncoding("GB2312"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" > <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" rel="external nofollow" rel="external nofollow" > --> <script type="text/javascript"> window.onload = function(){ //獲取submit var submit = document.getElementById("submit"); var name = document.getElementById("name"); //為submit繫結單擊響應函式 submit.onclick = function(){ times = document.getElementsByName("time"); var count=0; for(var i=0;i<times.length;i++){ if(times[i].checked == true){ count++; } } if(count>=2){ alert("只能選擇一個選項"); return false; } }; }; </script> </head> <body> <!-- 設定html頁面 --> <form action="sucess.jsp" method="post"> 使用者名稱:<input name="username" /><br/> <input type="checkbox" name="time" value="notSave" />不儲存 <input type="checkbox" name="time" value="aDay" />一天 <input type="checkbox" name="time" value="aWeek" />一週 <input type="checkbox" name="time" value="forever" />永久 <br/><br/> <input type="submit" name="submit" id="submit" value="登入"/> </form> <% //讀取session值 String val= (String)session.getAttribute("name"); //如果session不存在 if(val==null){ val ="不存在"; } out.print("當前\""+val+"\"使用者可自動登入"); %> </body> </html>
sucess.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%>" rel="external nofollow" rel="external nofollow" > <title>My JSP 'show.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,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" > --> </head> <body> <% //獲取username String name = request.getParameter("username"); //判斷使用者名稱是否存在 if(name != null && !name.trim().equals("")){ String[] time = request.getParameterValues("time"); //設定session值,便於login頁面讀取 session.setAttribute("name",name); //設定Cookie Cookie Cookie = new Cookie("name",name); //根據提交選項設定cookie儲存時間 if(time != null){ for(int i=0;i<time.length;i++){ //不儲存Cookie if(time[i].equals("notSave")){ Cookie.setMaxAge(0); } //儲存一天Cookie if(time[i].equals("aDay")){ Cookie.setMaxAge(60*60*24); } //儲存一週Cookie if(time[i].equals("aWeek")){ Cookie.setMaxAge(60*60*24*7); } //永久儲存Cookie,設定為100年 if(time[i].equals("forever")){ Cookie.setMaxAge(60*60*24*365*100); } } } //在客戶端儲存Cookie response.addCookie(Cookie); } else{%> <%--使用者名稱不存在則進行判斷是否已有cookie --%> <% //獲取cookie Cookie[] cookies = request.getCookies(); //cookie存在 if(cookies != null && cookies.length > 0){ for(Cookie cookie:cookies){ //獲取cookie的名字 String cookieName = cookie.getName(); //判斷是否與name相等 if(cookieName.equals("name")){ //獲取cookie的值 String value = cookie.getValue(); name = value; } } } } if(name != null && !name.trim().equals("")){ out.print("您好: " + name+"歡迎登入"); } else{//否則重定向到登入介面 out.print("您還沒有註冊,2秒後轉到註冊介面!"); response.setHeader("refresh","2;url=login.jsp"); %> 如果沒有自動跳轉,請點選<a href="login.jsp" rel="external nofollow" >此處</a>進行跳轉 <% //response.sendRedirect("login.jsp"); } %> </body> </html>
實現效果:
1.
2.
3.
4.
5.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。