一:核心原理介绍
1.介绍(包括核心机制)
是客户端保持http状态信息的方案
由WEB服务器在http响应消息头请求中附带的传递给浏览器的一个小文本文件
一旦浏览器保存了某个cookie,那么在以后每次的访问服务器时,都会在http请求头将这个cookie回传给服务器
cookie只能标识一种信息,一个name和value
cookie存储在浏览器的内存中,浏览器被关掉后,浏览器端的cookie被删除
2.程序(产生与获取cookie)
读取只能读取到的是数组,通过遍历找到需要的cookie。
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 //获取cookie 12 Cookie[] cookies=request.getCookies(); 13 if(cookies!=null&&cookies.length>0){ 14 for(Cookie cook:cookies){ 15 out.println(cook.getName()+":"+cook.getValue()); 16 } 17 }else{ 18 out.println("没有cookie,正在创建并返回"); 19 //创建一个cookie 20 Cookie cookie=new Cookie("name","abcd"); 21 //将cookie传递给客户端 22 response.addCookie(cookie); 23 } 24 %> 25 </body> 26 </html>
3.效果
4.会话Cookie的发送
发送cookie需要HttpServletResponse的addCookie方法,将cookie插入到一个Set-Cookie响应包头中,这个方式不修改之前的报头,而是创建新的报头,因此,称之为addCookie,而不是setCookie。
5.持久cookie
如果设置了时间,浏览器就会把cookie保存到硬盘上,关闭后再次打开浏览器,这些cookie依然有效。
存储在硬盘上的cookie可以在不同的浏览器上进行共享
cookie.setMaxAge(30)方式来设置,以秒为单位,如果为0表示立刻删除,如果是负数,表示不存储,为正数,表示cookie存储的时间。
6.程序
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 //获取cookie 12 Cookie[] cookies=request.getCookies(); 13 if(cookies!=null&&cookies.length>0){ 14 for(Cookie cook:cookies){ 15 out.println(cook.getName()+":"+cook.getValue()); 16 } 17 }else{ 18 out.println("没有cookie,正在创建并返回"); 19 //创建一个cookie 20 Cookie cookie=new Cookie("name","abcd"); 21 cookie.setMaxAge(30); 22 //将cookie传递给客户端 23 response.addCookie(cookie); 24 } 25 %> 26 </body> 27 </html>
7.效果
再次打开浏览器,cookie依然存在。
二:自动登录
1.login.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="index.jsp" method="post"> 11 Name:<input type="text" name="userName"/> 12 <input type="submit" value="Submit"/> 13 </form> 14 </body> 15 </html>
2.index.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 如果没有请求参数,也没有cookie中寻找到合适的参数,就返回去登录,因此 11 只要有其中一个就可实现自动登录 --> 12 <% 13 String name=request.getParameter("userName"); 14 if(name!=null&&!name.trim().equals("")){ 15 // 16 Cookie cookie=new Cookie("userName",name); 17 cookie.setMaxAge(50); 18 response.addCookie(cookie); 19 }else{ 20 Cookie[] cookies=request.getCookies(); 21 if(cookies!=null&&cookies.length>0){ 22 for(Cookie cook:cookies){ 23 String cookieName=cook.getName(); 24 if("userName".equals(cookieName)){ 25 String val=cook.getValue(); 26 name=val; 27 } 28 } 29 } 30 } 31 if(name!=null&&!name.trim().equals("")){ 32 out.print("hello"+name); 33 }else{ 34 response.sendRedirect("login.jsp"); 35 } 36 %> 37 </body> 38 </html>
3.效果
在一定的时间内,不需要到login.jsp登录,打开浏览器就可以直接进这个页面。
三:显示最近浏览的物品
1.books.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h2>Books Page</h2><br/><br/> 11 <a href="book.jsp?book=JavaWeb">JavaWeb</a><br/> 12 <a href="book.jsp?book=Java">Java</a><br/> 13 <a href="book.jsp?book=Oracle">Oracle</a><br/> 14 <a href="book.jsp?book=H5">H5</a><br/> 15 <a href="book.jsp?book=AJax">AJax</a><br/> 16 <a href="book.jsp?book=JDBC">JDBC</a><br/> 17 <a href="book.jsp?book=Spring">Spring</a><br/><br/><br/> 18 19 <!-- --> 20 <% 21 Cookie[] cookies=request.getCookies(); 22 23 if(cookies!=null&&cookies.length>0){ 24 for(Cookie cook:cookies){ 25 String name=cook.getName(); 26 if(name.startsWith("AT_TOP")){ 27 out.println(cook.getValue()); 28 out.println("<br>"); 29 } 30 } 31 } 32 %> 33 </body> 34 </html>
2.book.jsp
1 <%@page import="java.util.List"%> 2 <%@page import="java.util.ArrayList"%> 3 <%@ page language="java" contentType="text/html; charset=utf-8" 4 pageEncoding="utf-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <h2>Book Details</h2> 13 Book:<%=request.getParameter("book") %><br><br> 14 15 <a href="books.jsp">Return</a> 16 17 <% 18 //获取request传进来的book 19 String book=request.getParameter("book"); 20 /* 21 确定要删除的Cookie 22 */ 23 Cookie[] cookies=request.getCookies(); 24 List<Cookie> bookCookies=new ArrayList<>(); 25 Cookie tmpCookie=null;//存放和book.jsp传入的book匹配的那个cookie 26 if(cookies!=null&&cookies.length>0){ 27 for(Cookie cook:cookies){ 28 String name=cook.getName(); 29 if(name.startsWith("AT_TOP")){ 30 bookCookies.add(cook); 31 if(cook.getValue().equals(book)){ 32 tmpCookie=cook; 33 } 34 } 35 } 36 } 37 if(bookCookies.size()>=5&&tmpCookie==null){ 38 tmpCookie=bookCookies.get(0); 39 } 40 if(tmpCookie!=null){ 41 tmpCookie.setMaxAge(0); 42 response.addCookie(tmpCookie); 43 } 44 /* 45 将book.jsp获得的book作为cookie进行回传 46 */ 47 Cookie cookie=new Cookie("AT_TOP"+book,book); 48 response.addCookie(cookie); 49 %> 50 </body> 51 </html>
3.效果
四:Cookie路径
1.结论
作用范围:可以作用当前目录与当前目录的子目录,但是,不能作用与当前目录的上一级目录。
2.解决上面的问题
设置cookie的作用范围:
cookie.setPath(request.getContextPath());
然后:
response.addCookie(cookie);
就可以在上一级获取到在此处增加的cookie。
其中:
/ 表示站点
request.getContextPath()表示应用的根目录。