1、cookie的出现
1)当前的问题
在浏览器发送不同请求时,服务器端基于HTTP协议无法识别多次请求是否来自同一个浏览器
2)解决方法情境举例
一个咖啡厅促销活动:每位顾客喝5杯咖啡赠送一杯
方法一:
让服务员把喝过咖啡的顾客都记住——不现实。
方法二:
给每位顾客办一张会员卡,在卡片上记录他消费的次数
将消费的次数保存在顾客手中——Cookie:在浏览器端缓存数据的一种技术
0001:3→咖啡厅
0002:4→咖啡厅
0003:1→咖啡厅
方法三:
给每位顾客办一张会员卡,卡片上只有一个卡号,咖啡厅根据卡号保存顾客消费的次数
将消费的次数保存在咖啡厅——Session:在服务器端缓存数据的技术
0001→咖啡厅[0001:3]
0002→咖啡厅[0002:5]
0003→咖啡厅[0003:1]
2、cookie的运行机制
1)Cookie运行机制
①“办卡”:创建Cookie对象
Cookie cookie = new Cookie("myCookie", UUID.randomUUID().toString());
②“将卡片交到顾客手中”:将Cookie对象中封装的信息回送到浏览器
API:response.addCookie(cookie);
以响应消息头的形式发送给浏览器
Set-Cookie:myCookie=5691a6b4-7441-4916-b319-1a04c31ec504
③“顾客带着卡片来咖啡厅”:浏览器请求服务器时携带Cookie数据
以请求消息头的形式携带Cookie数据
Cookie:myCookie=5691a6b4-7441-4916-b319-1a04c31ec504
④“咖啡厅检查顾客卡片”:服务器从请求对象中获取Cookie数据
//从request对象中能够获取到Cookie信息,返回的是Cookie的数组
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
Cookie[] cookies = request.getCookies();
//cookies数组不一定有值,如果浏览器没有提交Cookie数据,则cookies的值就等于null
if(cookies != null) {
for(int i = 0; i < cookies.length; i++) {
String name = cookies[i].getName();
String value = cookies[i].getValue();
writer.write("Cookie中的数据是:"+name+"="+value);
writer.write("<br />");
}
}
2)不同浏览器
不同的浏览器访问同一个服务器,服务器也会根据浏览器的不同,返回不同的Cookie
3)Cookie有效性
(1)分类
[1]会话级Cookie:保存在浏览器内存中,随着浏览器关闭的关闭而释放,默认情况
[2]持久化级Cookie:保存在文件系统中,不会随着浏览器关闭而释放,但到达预先指定的时间就会自动释放
(2)设置
cookie.setMaxAge(int seconds);
[1]seconds正整数:表示将Cookie设置为持久化Cookie,保持seconds秒
[2]seconds=0:表示删除Cookie
[3]seconds负整数:表示将Cookie设置为会话级Cookie
注意:设置完成以后,必须使用response的addCookie()添加才可生效
3、code
1)CreateCookieServlet
package com.alex.web.servlet.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//创建Cookie
public class CreateCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 写cookie
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie cookie = new Cookie("username", "alex");
response.addCookie(cookie);
}
}
2)DeleteCookieServlet
package com.alex.web.servlet.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DeleteCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 删除cookie, cookie没有delete 或者remove 等方法,
// 要想删除,只能重新赋值或者设置失效时间
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie cookie = new Cookie("username", null);
// 设置cookie的失效时间
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
3)GetCookieServlet
package com.alex.web.servlet.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 读取cookie
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
if ("username".equals(name)) {
String value = cookie.getValue();
System.out.println(name + " -> " + value);
}
}
}
}
}
4)PersistCookieServlet
package com.alex.web.servlet.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//创建持久性cookie
public class PersistCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
//写cookie
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("username","alex");
//设置cookie的失效时间
cookie.setMaxAge(60*60);
//如果添加路径,那么只有访问某某路径下的时候,cookie才会跟着走
//cookie.setPath(request.getContextPath() + "/xxx");
response.addCookie(cookie);
}
}
5)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>web_cookie</display-name>
<servlet>
<servlet-name>CreateCookieServlet</servlet-name>
<servlet-class>com.alex.web.servlet.cookie.CreateCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateCookieServlet</servlet-name>
<url-pattern>/CreateCookieServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetCookieServlet</servlet-name>
<servlet-class>com.alex.web.servlet.cookie.GetCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetCookieServlet</servlet-name>
<url-pattern>/GetCookieServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>PersistCookieServlet</servlet-name>
<servlet-class>com.alex.web.servlet.cookie.PersistCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PersistCookieServlet</servlet-name>
<url-pattern>/PersistCookieServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DeleteCookieServlet</servlet-name>
<servlet-class>com.alex.web.servlet.cookie.DeleteCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteCookieServlet</servlet-name>
<url-pattern>/DeleteCookieServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
6)testcookie.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>
<a href="CreateCookieServlet">创建Cookie</a>
</h3>
<h3>
<a href="GetCookieServlet">访问Cookie</a>
</h3>
<h3>
<a href="PersistCookieServlet">设置持久化Cookie</a>
</h3>
<h3>
<a href="DeleteCookieServlet">删除指定的Cookie</a>
</h3>
</body>
</html>
网友评论