Cookie概述
-
Cookie:客户端会话管理技术
- 把要共享的数据保存到客户端(浏览器)
- 每次请求时,把会话信息带到服务器端,从而实现多次请求的数据共享
-
作用:可以保存客户端访问网站的相关内容,从而保证每次访问时先从本地缓存中获取,以此提高效率!
Cookie的常见属性
属性名称 | 属性作用 | 是否重要 |
---|---|---|
name | cookie的名称 | 必要属性 |
value | cookie的值(不能是中文) | 必要属性 |
path | cookie的路径 | 重要 |
domain | cookie的域名 | 重要 |
maxAge | cookie的生存时间 | 重要 |
version | cookie的版本号 | 不重要 |
comment | cookie的说明 | 不重要 |
细节
Cookie有大小,个数限制。每个网站最多只能存20个cookie,且大小不能超过4kb。同时,所有网站的cookie总数不超过300个。
当删除Cookie时,设置maxAge值为0。当不设置maxAge时,使用的是浏览器的内存,当关闭浏览器之后,cookie将丢失。设置了此值,就会保存成缓存文件(值必须是大于0的,以秒为单位)。
Cookie的方法和添加获取
-
cookie的方法
方法名 作用 Cookie(String name,String value) 构造方法创建对象 -
Cookie源码
public Cookie(String name, String value) { validation.validate(name); this.name = name; this.value = value; } public void setComment(String purpose) { this.comment = purpose; } public String getComment() { return this.comment; } public void setDomain(String pattern) { this.domain = pattern.toLowerCase(Locale.ENGLISH); } public String getDomain() { return this.domain; } public void setMaxAge(int expiry) { this.maxAge = expiry; } public int getMaxAge() { return this.maxAge; } public void setPath(String uri) { this.path = uri; } public String getPath() { return this.path; } public void setSecure(boolean flag) { this.secure = flag; } public boolean getSecure() { return this.secure; } public String getName() { return this.name; } public void setValue(String newValue) { this.value = newValue; } public String getValue() { return this.value; } public int getVersion() { return this.version; } public void setVersion(int v) { this.version = v; }
-
添加:HttpServletResponse对象中提供了addCookie()方法
返回值 方法名 作用 void addCookie(Cookie cookie) 向客户端添加Cookie -
获取:HttpServletRequest对象中提供了getCookies()方法
返回值 方法名 作用 Cookie[ ] getCookies() 获取所有的Cookie -
补充:为啥是响应对象添加cookie,而获取对象是请求对象?
- 因为,cookie是通过服务器端代码将数据保存在浏览器,所以服务器让浏览器做一件事情,肯定是通过响应
- 而在浏览器存储的数据,要想告诉服务器都有什么,那肯定是通过请求
Cookie的使用细节
- 数量限制
- 每个网站最多只能有 20 个 Cookie,且大小不能超过 4KB。所有网站的 Cookie 总数不能超过 300 个。
- 名称限制
- Cookie 的名称只能包含 ASCCI 码表中的字母、数字字符。不能包含逗号、分号、空格,不能以 $ 开头。
- Cookie 的值不支持中文。
- 存活时间限制 setMaxAge() 方法接收数字
- 负整数:当前会话有效,浏览器关闭则清除。
- 0:立即清除。
- 正整数:以秒为单位设置存活时间。
- 访问路径限制
- 默认路径:取自第一次访问的资源路径前缀。只要以这个路径开头就能访问到
- 设置路径:setPath() 方法设置指定路径
Cookie访问路径限制
- 案例展示:
@WebServlet("/servlet/servletDemo02")
public class ServletDemo02 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建Cookie并添加
Cookie cookie = new Cookie("username","zhangsan");
cookie.setMaxAge(3600);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
@WebServlet("/servlet/servletDemo03")
public class ServletDemo03 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取Cookie
Cookie[] arr = req.getCookies();
for(Cookie c : arr) {
if("username".equals(c.getName())) {
String value = c.getValue();
resp.getWriter().write(value);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
@WebServlet("/servlet/aaa/servletDemo04")
public class ServletDemo04 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取Cookie
Cookie[] arr = req.getCookies();
for(Cookie c : arr) {
if("username".equals(c.getName())) {
String value = c.getValue();
resp.getWriter().write(value);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
@WebServlet("/bbb/servletDemo05")
public class ServletDemo05 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取Cookie
Cookie[] arr = req.getCookies();
for(Cookie c : arr) {
if("username".equals(c.getName())) {
String value = c.getValue();
resp.getWriter().write(value);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
-
访问结果:
ServletDemo03、ServletDemo04可以访问到
zhangsan
,ServletDemo05访问不到数据zhangsan
。 -
结论:Cookie的路径限制
- 取自第一次访问的资源路径前缀
- 只要以这个前缀为开头(包括子级路径)就可以获取到
- 反之获取不到