领略并把握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+"https://www.jb51.net/"; %> <%request.setCharacterEncoding("GB2312"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>" > <title>My JSP 'https://www.jb51.net/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 type="text/css" href="https://www.jb51.net/styles.css" > --> <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 /><br/> <input type="checkbox" value="notSave" />不生存 <input type="checkbox" value="aDay" />一天 <input type="checkbox" value="aWeek" />一周 <input type="checkbox" value="forever" />永久 <br/><br/> <input type="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+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>" > <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 type="text/css" href="https://www.jb51.net/styles.css" > --> </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=https://www.jb51.net/login.jsp"); %> 假如没有自动跳转,请点击<a href="https://www.jb51.net/login.jsp" >此处</a>举办跳转 <% //response.sendRedirect("https://www.jb51.net/login.jsp"); } %> </body> </html>
实现结果:
1.
2.
3.
4.
5.