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()
-
- cookie.setMaxAge(-1); // -1为内存cookie(负数为内存cookie)
- cookie.setMaxAge(0); // cookie立刻失效
- cookie.setMaxAge(5); // cookie存活5秒(正数为硬盘cookie的存活时间)
js中:
在默认情况下,cookie是临时存在的。在一个浏览器窗口打开时,可以设置cookie,只要该浏览器窗口没有关闭,cookie就一直有效,而一旦浏览器窗口关闭后,cookie也就随之消失。
如果想要cookie在浏览器窗口关闭之后还能继续使用,就需要为cookie设置一个生存期。所谓生存期也就是cookie的终止日期,在这个终止日期到达之前,浏览器随时都可以读取该cookie。一旦终止日期到达之后,该cookie将会从cookie文件中删除。
- /*
- 功能:保存cookies函数
- 参数:name,cookie名字;value,值
- */
- function SetCookie(name,value){
- var Days = 60; //cookie 将被保存两个月
- var exp = new Date(); //获得当前时间
- exp.setTime(exp.getTime() + Days*24*60*60*1000); //换成毫秒
- document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
- }
- /*
- 功能:获取cookies函数
- 参数:name,cookie名字
- */
- function getCookie(name){
- var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
- if(arr != null)
- return unescape(arr[2]);
- return null;
- }
- /*
- 功能:删除cookies函数
- 参数:name,cookie名字
- */
- function delCookie(name){
- var exp = new Date(); //当前时间
- exp.setTime(exp.getTime() - 1);
- var cval=getCookie(name);
- if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
- }