Cookie在多个请求中丢失(spring mvc + jsp)

时间:2013-06-23 19:16:24

标签: java jsp spring-mvc cookies

我在我的控制器中设置我的cookie,返回一个新的modelandview,cookie被设置。但是,在任何其他请求中,cookie都将丢失。

此外,当我将cookie重置为某个其他值时,该值在加载页面时不会更改。它在页面刷新后会发生变化,并且在任何其他请求时都会丢失。

在所有这些多个请求中,JSESSIONID保持不变。

在控制器中:

Cookie locationCookie = new Cookie("locCookie", loc);
locationCookie.setMaxAge(60*60*24*365); //one year
response.addCookie(locationCookie);

return FWD_HOME;

在JSP(FWD_HOME)中:

<jsp:include page="/WEB-INF/jsp/fragments/header.jsp"></jsp:include>
<jsp:forward page="/HOME"></jsp:forward>

在JSP(标题)中:

        <%
           Cookie cookie = null;
           Cookie[] cookies = null;
           cookies = request.getCookies();
           String locValue = null;
           if( cookies != null ){
              for (int i = 0; i < cookies.length; i++){
                 cookie = cookies[i];
                 out.print(cookie.getName()+"=");
                 out.print(cookie.getValue()+";");
                 if("locCookie".equals(cookie.getName())){
                     locValue = cookie.getValue();
                 }
              }
          }
           out.print(locValue);
        %>

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我得到了解决方案。与Cookies on localhost with explicit domain一起,将路径设置为“/”使其正常工作。