简体   繁体   English

与Cookie.setMaxAge(Integer.MAX_VALUE)有什么关系?

[英]what's the deal with Cookie.setMaxAge(Integer.MAX_VALUE)?

setMaxAge is supposed to control the maximum age of a cookie, so if my intention is to literally set the maximum age possible the logical thing would be doing: setMaxAge应该控制cookie的最大年龄,所以如果我的意图是按字面设置可能的最大年龄,逻辑事物将会做:

cookie.setMaxAge(Integer.MAX_VALUE);

However this results in no Expires in cookie HTTP header at all, I'm testing it on a WebSphere 7. Setting it to something like 1 year measured in seconds works fine. 然而,这导致cookie HTTP头中没有Expires ,我在WebSphere 7上测试它。将它设置为1秒测量的工作正常。

Why is that happening? 为什么会这样?

PS Have just tested on Tomcat7 - Integer.MAX_VALUE results in Max-Age=2147483647; Expires=Wed, 06-Mar-2080 21:30:32 GMT PS刚刚在Tomcat7上测试过 - Integer.MAX_VALUE导致Max-Age=2147483647; Expires=Wed, 06-Mar-2080 21:30:32 GMT Max-Age=2147483647; Expires=Wed, 06-Mar-2080 21:30:32 GMT , so it must be something wrong with WebSphere. Max-Age=2147483647; Expires=Wed, 06-Mar-2080 21:30:32 GMT ,所以它必定是WebSphere的错误。

Problem is described in http://www-01.ibm.com/support/docview.wss?uid=swg1PM34869 . 问题在http://www-01.ibm.com/support/docview.wss?uid=swg1PM34869中描述。 Cause is a kind of millenium problem: WAS7 writes cookies date as YY, WAS6 as YYYY 原因是一种千年问题:WAS7将cookies日期写为YY,WAS6为YYYY

instead of setting the max age for a cookie, this setMaxAge method adds the int parameter seconds to the current SERVER date/time and puts this new date/time value to a cookie as its expiration time. 此setMaxAge方法不是设置cookie的最大年龄,而是将int参数seconds添加到当前SERVER日期/时间,并将此新的日期/时间值作为其到期时间放入cookie。 Later, when this cookie is sent to the browser, this expiration date is compared to the CLIENT date/time, and cookie gets expired if its expiration date is in past. 稍后,当此cookie发送到浏览器时,此过期日期将与CLIENT日期/时间进行比较,如果过期日期已过,则cookie将过期。

so instead of using setMaxAge(int) use method with the date and time. 所以不要使用setMaxAge(int)使用方法与日期和时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM