【发布时间】:2012-09-13 20:49:28
【问题描述】:
在我的 jsp 文件中,我设置了一个我想保留一年的 cookie。我已经使用了设置的最大年龄并乘以我的秒数,所以它是一年。然而,cookie 只持续大约 5 分钟。即使我输入了一个大得离谱的值,它仍然只能持续约 5 分钟。
我在 localhost 上进行测试,这与它有什么关系吗?
代码如下:
//value to check if the EU compliance cookie is present
boolean euCookiePresent = false;
//cookie parameters
String cookieName = "wtr_ca";
String cookieValue = "1";
int cookieExpiry = 365;
//cookie expiary is in seconds so convert to days
cookieExpiry = cookieExpiry * 60 * 60 * 24;
//Get clients cookies
Cookie cookies [] = request.getCookies ();
if (cookies != null){
//iterate over users cookies to check for euCookie
for (int i = 0; i < cookies.length; i++)
{
if (cookies [i].getName().equals (cookieName)){
euCookiePresent = true;
break;
}
}
}
if (euCookiePresent == false){
//no EU Cookie found therefore place it on client
response.setContentType("text/html");
Cookie cookie = new Cookie(cookieName,cookieValue);
//cookie expiary is in seconds so convert to days
cookie.setMaxAge(cookieExpiry);
response.addCookie(cookie);
}
【问题讨论】:
-
//cookie expiary is in seconds so convert to days实际上正好相反://cookie expiary is in seconds so convert from days to seconds。转换本身很好。 ;)