Java 我的cookie的最大值已重置为-1
我正在尝试创建一个cookie并设置其最大年龄。 我使用set Comment和set Max Age方法来设置相同的值Java 我的cookie的最大值已重置为-1,java,spring-mvc,session,cookies,Java,Spring Mvc,Session,Cookies,我正在尝试创建一个cookie并设置其最大年龄。 我使用set Comment和set Max Age方法来设置相同的值 HttpSession browserSession = httpRequest.getSession(); Cookie cookie = new Cookie("SESSION", browserSession.getId()); cookie.setComment("test"); cookie.setMaxAge(Integer.MAX_VALUE); ht
HttpSession browserSession = httpRequest.getSession();
Cookie cookie = new Cookie("SESSION", browserSession.getId());
cookie.setComment("test");
cookie.setMaxAge(Integer.MAX_VALUE);
httpResponse.addCookie(cookie);
但当我从请求中获取cookies,然后调试它时,注释为null,cookie的最大年龄为-1,即我设置为会话的cookie名称。
为什么会发生这种情况?这是预期的行为。考虑到max age应用于客户端,但客户端只向服务器发送pair name值 因此,如果您将cookie设置为“3600”并将其添加到响应中,您应该可以在客户端的cookie上看到,但是在下一个请求中包含的cookie可能会在服务器端显示,-1
有关更多信息,请参阅。我怀疑您需要一个
CookieManager
对象来正确执行此操作。C#是一样的;它需要一个CookieContainer
对象。另请参见,请注意,如果integer
是无符号的,则将有符号整数设置为integer.MAX_值很可能会导致-1。见相关文件: