cookie的生命周期

java中:

--------------------------------------------------------

setMaxAge

public void setMaxAge(int expiry)

Sets the maximum age of the cookie in seconds.

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 cookie
See Also:
getMaxAge()

   ========

setMaxAge

public void setMaxAge(long expiry)

 

设置 cookie 的最大生存时间,以秒为单位。

正值表示 cookie 将在经过该值表示的秒数后过期。注意,该值是 cookie 过期的最大 生存时间,不是 cookie 的当前生存时间。

负值意味着 cookie 不会被持久存储,将在 Web 浏览器退出时删除。0 值会导致删除 cookie。

 

参数:
expiry - 指定 cookie 最大生存时间的整数,以秒为单位;如果为 0,则应立即丢弃 cookie;否则,cookie 的最大生存时间没有指定。
另请参见:
getMaxAge()

 

Java代码   收藏代码
  1. cookie.setMaxAge(-1); // -1为内存cookie(负数为内存cookie)   
  2. cookie.setMaxAge(0); // cookie立刻失效   
  3. cookie.setMaxAge(5); // cookie存活5秒(正数为硬盘cookie的存活时间)  

 

 

js中:

 在默认情况下,cookie是临时存在的。在一个浏览器窗口打开时,可以设置cookie,只要该浏览器窗口没有关闭,cookie就一直有效,而一旦浏览器窗口关闭后,cookie也就随之消失。
如果想要cookie在浏览器窗口关闭之后还能继续使用,就需要为cookie设置一个生存期。所谓生存期也就是cookie的终止日期,在这个终止日期到达之前,浏览器随时都可以读取该cookie。一旦终止日期到达之后,该cookie将会从cookie文件中删除。

  

Js代码   收藏代码
  1. /* 
  2.  
  3. 功能:保存cookies函数  
  4. 参数:name,cookie名字;value,值 
  5. */  
  6. function SetCookie(name,value){  
  7.     var Days = 60;   //cookie 将被保存两个月  
  8.     var exp  = new Date();  //获得当前时间  
  9.     exp.setTime(exp.getTime() + Days*24*60*60*1000);  //换成毫秒  
  10.     document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();  
  11. }   
  12. /* 
  13. 功能:获取cookies函数  
  14. 参数:name,cookie名字 
  15. */  
  16. function getCookie(name){  
  17.     var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));  
  18.     if(arr != null)  
  19.   return unescape(arr[2]);   
  20.     return null;  
  21.   
  22. }   
  23. /* 
  24. 功能:删除cookies函数  
  25. 参数:name,cookie名字 
  26. */  
  27.   
  28. function delCookie(name){  
  29.     var exp = new Date();  //当前时间  
  30.     exp.setTime(exp.getTime() - 1);  
  31.     var cval=getCookie(name);  
  32.     if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();  
  33. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值