在我的 jsp 文件中,我正在设置一个我想保留一年的 cookie。我使用了设置的最大年龄并乘以我的秒数,所以它是一年。然而,cookie 只持续大约 5 分钟。即使我输入了一个大得离谱的值,它仍然只能持续约 5 分钟。
我正在本地主机上进行测试,这可能与它有关吗?
下面的代码:
//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);
}