Basit
Basit

Reputation: 8606

Cookie value is not changing. Each time getting the previous value

I am using cookies. When you first time open the page i set the cookie like this

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

When first time you open the page then the condition if (sessionID == null && !sessionValid) becomes true and the userCookie set to value normal.Now if you close the browser tab, then open the page again. The cookie value is normal. OK.

But now when you log in then it comes to condition if (isRegisteredUser.equalsIgnoreCase(("1"))). Here i am trying to replace the userCookie value , like

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);

            }

But both the approaches are not working. Now after doing that if i close the browser, and open the page again, then in the condition

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

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

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

}

I am getting normal again as userCookie value..This time it should get me value "loginUser", because i have changed the value of userCookie to loginUser. BUt i am getting normal here. Why i am getting the previous value. what i am doing wrong? Please tell me.

Thanks

Upvotes: 1

Views: 6842

Answers (2)

Basit
Basit

Reputation: 8606

set the cookie path, solve the problem like

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

After setting cookie path, value is replaced.

Thanks

Upvotes: 4

DRCB
DRCB

Reputation: 2121

If you a cookie do not have an expiry or max age defined - it is considered a session cookie, which will be removed automatically when user closes his browser. See http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age

You have to set an expiry date for your cookie. See: 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);

Upvotes: 0

Related Questions