Cookie值没有变化。每次都获得前一个值

时间:2012-04-16 12:08:04

标签: java-ee jsf-2

我正在使用cookies。当您第一次打开页面时,我会像这样设置cookie

public class SessionTimeoutFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {

        Object isRegistered = session.getAttribute("logedin");
         if (isRegistered != null) {
             String isRegisteredUser = isRegistered.toString();
             if (isRegisteredUser.equalsIgnoreCase(("1"))) {
                 sessionID = UUID.randomUUID().toString();               
                 session.setMaxInactiveInterval(240);   //4min
                 Cookie userCookie = getCookie(httpServletRequest, "userCookie");
                 if (userCookie != null) {

                      //Value not setting here
                      Cookie loginUserCookie = new Cookie("userCookie", "loginUser");
                      httpServletResponse.addCookie(loginUserCookie);
                 }
                 filterChain.doFilter(httpServletRequest, httpServletResponse);
             }
         } else {
             sessionID = httpServletRequest.getRequestedSessionId();
             sessionValid = httpServletRequest.isRequestedSessionIdValid();

            //User open his browser
            if (sessionID == null && !sessionValid) {
                sessionID = UUID.randomUUID().toString();
                Cookie browserCookie = new Cookie("browserCookie", sessionID);
                httpServletResponse.addCookie(browserCookie); 
                Cookie userCookie = new Cookie("userCookie", "normal");
                httpServletResponse.addCookie(userCookie);

                session.setAttribute("logedin", "0");
                filterChain.doFilter(httpServletRequest, httpServletResponse);

            //Session expires. Each time user close the tab and session expires automatically
            } else if(sessionID != null && !sessionValid) { 
                if (httpServletRequest.isRequestedSessionIdFromCookie()) {
                    Cookie userCookie = getCookie(httpServletRequest, "userCookie");
                    String value = userCookie.getValue();

                    //Each time getting normal
                    if (value.equalsIgnoreCase("normal")) {
                        session.setAttribute("logedin", "0");
                        filterChain.doFilter(httpServletRequest, httpServletResponse);
                    } else if (value.equalsIgnoreCase("loginUser")) {

                    }
                } //end of if (httpServletRequest.isRequestedSessionIdFromCookie())
            }
         }
   } //end of dofilter()
} //end of class SessionTimeoutFilter

首次打开页面时,条件if (sessionID == null && !sessionValid)变为true,userCookie设置为normal normal.Now如果关闭浏览器选项卡,则再次打开页面。 cookie值正常。行。

但是现在当你登录时,它会进入条件if (isRegisteredUser.equalsIgnoreCase(("1")))。在这里,我试图替换userCookie值,如

Cookie userCookie = getCookie(httpServletRequest, "userCookie");

            if (userCookie != null) {

                String value = userCookie.getValue();

                //delete the cokie
                //userCookie.setValue("loginUser");
                //userCookie.setMaxAge(0);

                Cookie loginUserCookie = new Cookie("userCookie", "loginUser");
                httpServletResponse.addCookie(loginUserCookie);

            }

但两种方法都不起作用。现在,如果我关闭浏览器,再次打开页面,那么在条件

if (value.equalsIgnoreCase("normal")) {

    session.setAttribute("logedin", "0");
    filterChain.doFilter(httpServletRequest, httpServletResponse);

} else if (value.equalsIgnoreCase("loginUser")) {

}

我再次恢复正常,因为userCookie值。这次它应该让我的值为“loginUser”,因为我已经将userCookie的值更改为loginUser。我在这里变得正常。为什么我得到以前的价值。我做错了什么?请告诉我。

由于

2 个答案:

答案 0 :(得分:4)

设置cookie路径,解决问题,如

Cookie userCookie = new Cookie("userCookie", "loginUser");
userCookie.setPath("/");
httpServletResponse.addCookie(userCookie);

设置cookie路径后,将替换值。

由于

答案 1 :(得分:0)

如果您的cookie没有定义到期或最大年龄 - 它被视为会话cookie,当用户关闭浏览器时会自动删除。见http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age

您必须为Cookie设置到期日期。请参阅:http://docs.oracle.com/javaee/1.4/api/javax/servlet/http/Cookie.html#setMaxAge(int)

            Cookie loginUserCookie = new Cookie("userCookie", "loginUser");
            loginUserCookie.setMaxAge(3600); // cookie expires in an hour 
            httpServletResponse.addCookie(loginUserCookie);