4-cookie 简介
1.eclipse中tomcate镜像位置:
D:\javaTools\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tem1
2.cookie对象
作用:是能将客户的信息保存在客户端,是以文本的形式保存,生命周期由程序决定,可以很长
使用步骤:
a.创建一个cookie对象 :Cookie cook = new Cookie("key","value");
b.可以设置cookie的有效期:cook.setMaxAge();
c.将该cook对象添加到response对象中,发送(转发和重定向都可以)到前台页面
d.页面发送请求到服务端会带上cookie对象,服务端可以获取里面的信息来做相应的处理
课堂练习:
1.用cookie保存用户名,在成功页面获取显示
2.登录时课选择保存密码状态,下次访问该页面直接登录成功
login.jsp
1 <%@page import="com.pojo.UserInfo"%> 2 <%@page import="com.service.UserInfoService"%> 3 <%@page import="com.service.IUserInfoService"%> 4 <%@page import="java.net.CookieStore"%> 5 <%@ page language="java" contentType="text/html; charset=UTF-8" 6 pageEncoding="UTF-8"%> 7 <% 8 String path = request.getContextPath(); 9 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 10 %> 11 <!DOCTYPE html> 12 <html> 13 <head> 14 <base href="<%=basePath%>"/> 15 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 16 <title>Insert title here</title> 17 </head> 18 <% 19 Cookie[] cookies = request.getCookies(); 20 IUserInfoService userSer = new UserInfoService(); 21 if(cookies!=null) 22 for(int i=0;i<cookies.length;i++){ 23 if("username".equals(cookies[i].getName())){ 24 if(cookies[i].getValue()!=null&&!"".equals(cookies[i].getValue())){ 25 //根据用户名查询用户信息 26 UserInfo user = userSer.getUserByName(cookies[i].getValue()); 27 session.setAttribute("user", user); 28 response.sendRedirect("success.jsp"); 29 } 30 } 31 } 32 %> 33 <body> 34 <form action="cookie/control.jsp" method="post"> 35 用户名:<input name="username"><br> 36 密码:<input type="password" name="password"><br> 37 是否记住<input type="checkbox" name="isremder" value="1"><input type="submit" value="登录"> 38 </form> 39 </body> 40 </html>
control.jsp
1 <%@page import="com.pojo.UserInfo"%> 2 <%@page import="com.service.UserInfoService"%> 3 <%@page import="com.service.IUserInfoService"%> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <% 7 String path = request.getContextPath(); 8 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 9 %> 10 <!DOCTYPE html> 11 <html> 12 <head> 13 <base href="<%=basePath%>"/> 14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 15 <title>Insert title here</title> 16 </head> 17 <body> 18 <% 19 request.setCharacterEncoding("utf-8"); 20 String username = request.getParameter("username"); 21 String password = request.getParameter("password"); 22 String isrember = request.getParameter("isremder"); 23 IUserInfoService userSer = new UserInfoService(); 24 UserInfo user = userSer.login(username, password); 25 if(user!=null){ 26 session.setAttribute("user",user); 27 //登录成功,将用户名和密码保存到cookie中 28 if("1".equals(isrember)){//记住用户名 29 Cookie cookie = new Cookie("username",user.getUser_name()); 30 cookie.setMaxAge(1000*3600*24*7);//设置cookie的有效期 31 response.addCookie(cookie);//将cookie添加到响应中发送给客户端 32 } 33 response.sendRedirect("success.jsp"); 34 }else{ 35 response.sendRedirect("login.jsp?error=true"); 36 } 37 %> 38 </body> 39 </html>
success.jsp
<%@page import="com.pojo.UserInfo"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //获取cookie中的值 // Cookie[] cookies = request.getCookies(); // String username = ""; // String password = ""; // if(cookies!=null) // for(int i=0;i<cookies.length;i++){ // if("username".equals(cookies[i].getName())){ // username = cookies[i].getValue(); // } // if("password".equals(cookies[i].getName())){ // password = cookies[i].getValue(); // } // } //session中获取用户信息 UserInfo user = (UserInfo)session.getAttribute("user"); %> <h1>欢迎<%=user.getUser_name() %>登录 <a href="">退出登录</a> </h1> </body> </html>
exit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <% //获取username的cookie Cookie[] cookies = request.getCookies(); if(cookies!=null) for(int i=0;i<cookies.length;i++){ if("username".equals(cookies[i].getName())){ cookies[i].setMaxAge(0); response.addCookie(cookies[i]);//覆盖掉客户端的有效cookie } } response.sendRedirect("login.jsp"); %> <body> </body> </html>