Java 中的 Cookie 最大年龄
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23629868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cookie max age in Java
提问by Tony
What is the difference between this:
这有什么区别:
cookie.setMaxAge(0);
and this
和这个
cookie.setMaxAge(-1);
Does first make it removed?
先把它去掉吗?
采纳答案by IsidroGH
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.
负值表示 cookie 不会持久存储,将在 Web 浏览器退出时删除。零值会导致 cookie 被删除。
http://docs.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html#setMaxAge%28int%29
http://docs.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html#setMaxAge%28int%29
回答by nanofarad
From RFC 6265:
来自RFC 6265:
If delta-seconds is less than or equal to zero (0), let expiry-time be the earliest representable date and time. Otherwise, let the expiry-time be the current date and time plus delta-seconds seconds.
如果 delta-seconds 小于或等于零 (0),则让 expiry-time 为最早的可表示日期和时间。否则,让到期时间为当前日期和时间加上增量秒秒。
Therefore, both have the cookie expire as soon as possible on a compliant user-agent.
因此,两者都让 cookie在兼容的用户代理上尽快过期。
However, in practice, negative values imply session cookies.
然而,在实践中,负值意味着会话 cookie。
回答by Hirak
Assuming we are talking about javax.servlet.http.Cookie
假设我们正在谈论 javax.servlet.http.Cookie
This is what Javadocsays
这就是Javadoc所说的
setMaxAgepublic void setMaxAge(int expiry)
setMaxAge public void setMaxAge(int expiry)
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.
设置此 Cookie 的最长期限(以秒为单位)。
正值表示 cookie 将在经过多少秒后过期。请注意,该值是 cookie 将过期的最大年龄,而不是 cookie 的当前年龄。
负值表示 cookie 不会持久存储,将在 Web 浏览器退出时删除。
零值会导致 cookie 被删除。
回答by the-petrolhead
cookie.setMaxAge( 0 ) will delete the cookie right away.
cookie.setMaxAge( 0 ) 将立即删除 cookie。
cookie.setMaxAge( -1 ) will preserve the cookie for a while and delete the cookie when the browser exits.
cookie.setMaxAge( -1 ) 会将 cookie 保留一段时间,并在浏览器退出时删除 cookie。
For relevant information refer the API Documentation.
有关相关信息,请参阅API 文档。