Why does cookie.getMaxAge() in JSP always get -1? Do you understand? ? ? I've been depressed for a day and now I understand

I don't know what's going on. The question I posted the code didn't show up.
I use a page setting to add cookies to the client,
Use another page to get the cookie, you can get the value, only getValue(); The return value is correct,
All other values ​​are defaulted to the same, time has become -1., domain and path have become null, what is the problem? Is it a version problem?
The environment is:
I use ie6 and ie8, the result is the same, and the server is tomcat
You seem to be saying the same thing as what I found. . . I really didn't understand it so I asked. . . I set the path mentioned on the 4th floor, but the next time I read the path value becomes null,

Then after add. I use the output statement to output their values ​​are correct,

Cookie process 

new Cookie()->response ->client ->request -> getCookies If the PATH you set is correct, you can get it correctly

I also encountered this problem, also very confused, asked the same. My code ( JSP ):
The part that writes the cookie:

Cookie cookie = new Cookie("write", "cookie_write");
cookie.setComment("a cookie test");
cookie.setVersion(100);
cookie.setMaxAge(60 * 60 * 1000);
String host=request.getHeader("host");
if(host.indexOf(":")>0){
host=host.split(":")[0];
}
cookie.setDomain (host);
cookie.set Path ("/");//I have tried removing and leaving this line, both are the same
response.addCookie(cookie);

I left a breakpoint here at the end, and found that including MaxAge and the like are all correct of.

Read the part:
Cookie cookies[] = request.getCookies();
if (cookies != null) {
out.println("cookie:" + cookies.length);
out.
for (int i = 0; i < cookies.length; i++) {
out.println("getName=" + cookies[i].getName() + "<br>");
out.println("getValue=" + cookies[i].getValue() + "<br>");
out.println("getComment=" + cookies[i].getComment() + "<br>");
out.println("getDomain=" + cookies[i].getDomain() + "<br>");
out.println("getPath=" + cookies[i].getPath() + "<br>");
out.println("getPath=" + cookies[i].getPath() + "<br>");
out.println("getMaxAge=" + cookies[i].getMaxAge() + "<br>");
out.println("getVersion=" + cookies[i].getVersion() + "<br>");
out.println("getSecure=" + cookies[i].getSecure() + "<br>");
out.println("<br>" + "<br>");
}
}

读的这儿就不对了,都是null或者-1(MaxAge),但是能关了ie再开都能读到表示没过期(如果前面setMaxAge设置为-1,关了就没了)。

我也很疑惑,不知道有没有人一起研究一下。

-1表示该cookie生命周期为浏览器进程,浏览器关闭时该cookie会自动消亡。

貌似默认就是-1,与浏览器同在

你可以在tomcat等那些服务器上修改,祝你好运

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325551302&siteId=291194637