jsp實現(xiàn)用戶自動登錄功能
理解并掌握cookie的作用以及利用cookie實現(xiàn)用戶的自動登錄功能,實現(xiàn)下圖效果
當服務(wù)器判斷出該用戶是首次登錄的時候,會自動跳轉(zhuǎn)到登錄界面等待用戶登錄,并填入相關(guān)信息。通過設(shè)置cookie的有效期限來保存用戶的信息,關(guān)閉瀏覽器后,驗證是否能夠自動登錄,若能登錄,則打印歡迎信息;否則跳轉(zhuǎn)到登錄頁面。
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綁定單擊響應函數(shù) 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> <!-- 設(shè)置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,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" > --> </head> <body> <% //獲取username string name = request.getparameter("username"); //判斷用戶名是否存在 if(name != null && !name.trim().equals("")){ string[] time = request.getparametervalues("time"); //設(shè)置session值,便于login頁面讀取 session.setattribute("name", name); //設(shè)置cookie cookie cookie = new cookie("name",name); //根據(jù)提交選項設(shè)置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,設(shè)置為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秒后轉(zhuǎn)到注冊界面!"); response.setheader("refresh","2;url=login.jsp"); %> 如果沒有自動跳轉(zhuǎn),請點擊<a href="login.jsp" rel="external nofollow" >此處</a>進行跳轉(zhuǎn) <% //response.sendredirect("login.jsp"); } %> </body> </html>
實現(xiàn)效果:
1.
2.
3.
4.
5.
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持碩編程。
相關(guān)文章
- jsp+servlet實現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計的實現(xiàn)方法
- 將properties文件的配置設(shè)置為整個Web應用的全局變量實現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
- 詳解JSP 內(nèi)置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現(xiàn)局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎(chǔ)實踐
- JavaServlet的文件上傳和下載實現(xiàn)方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法