【发布时间】:2018-10-28 01:44:36
【问题描述】:
我正在尝试创建一个 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);
httpResponse.addCookie(cookie);
但是当我从 Request 中获取 cookie 然后我对其进行调试时,comment 为 null 并且 cookie 的最大年龄为 -1,我将 cookie 名称设置为 SESSION。 为什么会这样?
【问题讨论】:
-
我怀疑您需要一个
CookieManager对象才能正确执行此操作。 C#也是一样;它需要一个CookieContainer对象。见docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/… -
另外,请注意,如果
Integer是无符号的,将 signed 整数设置为 Integer.MAX_VALUE 很可能会导致 -1。见binaryconvert.com/result_signed_int.html?decimal=045049
标签: java spring-mvc session cookies