Cookie学习总结

Cookie有哪些属性

name cookie的名字
value cookie存储的值
maxAge cookie的销毁时间
path 该cookie在该path路径及其子目录下可以显示
domain cookie的域
comment cookie的注释,用来描述该cookie的目的
secure 是否基于https安全传输

下面重点总结下cookie的如下3个重要的方法:

cookie的setPath(String uri)方法

下面是Javadoc文档上的方法注释:
Specifies a path for the cookie to which the client should return the cookie.
为cookie指定路径,该路径是客户端(即浏览器)应该返回该cookie的路径。
The cookie is visible to all the pages in the directory you specify, and all the pages in that
该cookie对于你所指定的目录下的所有页面都是可见的,包括该目录的子目录下的所有页面。
directory’s subdirectories. A cookie’s path must include the servlet that set the cookie, for example,
一个cookie的路径必须包括设置该cookie的servlet,例如,/catalog,这使得该cookie对服务器中/catalog下
/catalog, which makes the cookie visible to all directories on the server under /catalog.
的所有目录可见。(备注下,对这句话我不是怎么理解,个人认为这种说法有误,因为我试验了下,在serveletA中设置了一个cookie,path设置为servletB所在的目录,该目录并没有包含servletA,然而显示创建成功了,在访问servletB时通过查看源可以看到该cookie对servletB可见的,访问servletA时,可以看到该cookie对servletA不可见。所以个人认为cookie的path不必设置为包含设置该cookie的servlet的目录)
Consult RFC 2109 (available on the Internet) for more information on setting path names for cookies.
Parameters:
uri a String specifying a path
See Also:getPath

cookie的path设置后,那么当我们访问位于该path路径下的所有页面时均可以见到该cookie,即在服务器端可以访问到客户端存储的该cookie。

**1.**cookie的path常常会设置为“/”,这使得该cookie对于该域上(即该网站或者说主机上)的所有web应用都可见, 这是因为“/”表示的是主机的web应用根目录。这里随便打开一个网页,如京东网站,查看其可见的cookie可以看到,基本都是设置为“/”。如下:
  

这里写图片描述
  

2.默认情况下,即如果你创建了一个cookie,没有显式地调用setPath(String uri)来设置cookie的话,cookie的path为当前页面所在的目录,即request.getServletContext()的返回值。这就意味着该cookie在该应用范围内都是可见的。(注意,上面所说的目录是指你访问当前页面的url路径,亦即逻辑路径,而不是指实际的存储目录或者说源代码目录,这是因为cookie一般都是在servlet,或者jsp中创建,通过response.addCookie(Cookie cookie)来设置cookie,返回到客户端存储起来。)
例如,session 默认情况下是基于cookie来实现唯一的标识的,当你在web中通过request.getSession(true)创建一个session时,该session会创建一个名为JSESSIONID的cookie,值为一个32位的16进制数,存储到客户端,域为网站域名,path为该web应用根路径,maxAge为会话。这样,当你浏览该应用的其他网页时,通过request.getSession(),借助SESSIONID这个cookie的值,就能够从服务器中的众多session中找到对应你这个会话的session.
  

这里写图片描述

  

3.当你设置path为request.getServletContext()/XXX路径时,那么该cookie就只对xxx目录下面的网页及其子目录下的网页可见,但是对其他目录下的网页就不可见,如request.getServletContext()/YYY,或者其父目录request.getServletContext()。这里request.getServletContext()亦即应用名。
如下,在lyons.eaby.new这个应用下,对于http://localhost:8080/lyons.eaby.new/lee/justfotest可见的cookie的path分别为/,/lyons.eaby.new,/lyons.eaby.new/lee,/lyons.eaby.new/lee/。而当你访问这个网站的这个应用的其他网页如http://localhost:8080/lyons.eaby.new/index.jsp时,上面客户端中存储的那些cookie,如/lyons.eaby.new/lee,/lyons.eaby.new/lee/就不可见了。
  

这里写图片描述
  
  

这里写图片描述


cookie的setMaxAge(int expiry)方法

setMaxAge(int expiry)
该方法用来设置cookie在客户端上的本地存储中的存活时间,以秒为单位,通常设置为10天。
1.设置为正值时,表明该cookie将会在过去那么多秒之后失效。
2.设置为负值时,意味着该cookie将不会持久化保存到本地,当浏览器退出时该cookie将被删除。
3.设置为0时,将导致该cookie被删除,而不是等到浏览器退出后再删除。
通常设置cookie的代码如下:首先获取到该cookie,然后设置maxAge为0,再通过response.addCookie(Cookie cookie)设置cookie达到删除cookie的目的。
cookie.setMaxAge(0);
response.addCookie(cookie);

Note:
俩个cookie相同当且仅当其domian,path均一致时。这在删除cookie时很重要,只有设置与你将要删除的cookie一致时才能达到正确地删除你想删除的cookie的目的。

下面是Javadoc文档上的方法注释:
Sets the maximum age in seconds for this Cookie.

A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie’s current age.

A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.

Parameters:expiry an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes the cookieSee Also:getMaxAge

cookie的setDomain()方法

待补充

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值