文章詳情頁
jsp cookie+session實(shí)現(xiàn)簡易自動(dòng)登錄
瀏覽:474日期:2022-06-08 09:08:29
本文實(shí)例為大家分享了jsp cookie+session實(shí)現(xiàn)簡易自動(dòng)登錄的具體代碼,供大家參考,具體內(nèi)容如下
關(guān)閉瀏覽器只會(huì)使存儲(chǔ)在客戶端瀏覽器內(nèi)存中的session cookie失效,不會(huì)使服務(wù)器端的session對(duì)象失效。
如果設(shè)置了過期時(shí)間,瀏覽器就會(huì)把cookie保存到硬盤上,關(guān)閉后再次打開瀏覽器,這些cookie依然有效直到超過設(shè)定的過期時(shí)間。
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"> <head> <title>登錄</title> </head> <body> <form action="sucess.jsp" method="post"> 用戶名:<input name="username" /><br/> <%--<input type="checkbox" name="time" />記住用戶名 --%> <input type="submit" name="submit" id="submit" value="登錄"/> </form> <% //讀取session值 String val= (String)session.getAttribute("name"); //如果session不存在 if(val==null){ val ="不存在"; } out.print("當(dāng)前\""+val+"\"用戶可自動(dòng)登錄"); %> </body></html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>主不在乎</title></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); Cookie.setMaxAge(30*24*3600); //設(shè)置cookie有效期為30天 response.addCookie(Cookie); //在客戶端保存Cookie out.println("welcome: " + name+"歡迎登錄"); } else{ response.sendRedirect("main.jsp"); } %><a href="login.jsp" >relogin</a></body></html>
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="ISO-8859-1"><title>主不在乎</title></head><body><%String name=(String)session.getAttribute("username");//獲取cookieCookie[] 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; } } out.println("welcome again: " + name+"歡迎登錄"); //************************* // 另一種寫法 String v=null; for(int i=0;i<cookies.length;i++){ if(cookies[i].getName().equals("name")){ v=cookies[i].getValue(); } } if(v!=null){ out.println(" Hello World "+v); } }//************************* else { response.sendRedirect("login.jsp"); }%><a href="login.jsp" >relogin</a></body></html>
運(yùn)行l(wèi)ogin.jsp
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
JSP
相關(guān)文章:
1. 詳解JSP 內(nèi)置對(duì)象request常見用法2. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式3. jsp實(shí)現(xiàn)登錄界面4. JSP靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入使用詳解5. SSM框架整合JSP中集成easyui前端ui項(xiàng)目開發(fā)示例詳解6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))8. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐9. Django Session和Cookie分別實(shí)現(xiàn)記住用戶登錄狀態(tài)操作10. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)
排行榜
