title: Cookie value is not changing. Each time getting the previous value
body: <p>I am using cookies. When you first time open the page i set the cookie like this</p>
  
  <pre><code>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 &amp;&amp; !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 &amp;&amp; !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
  </code></pre>
  
  <p>When first time you open the page then the condition <code>if (sessionID == null &amp;&amp; !sessionValid)</code> 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.</p>
  
  <p>But now when you log in then it comes to condition <code>if (isRegisteredUser.equalsIgnoreCase(("1")))</code>. Here i am trying to replace the userCookie value , like</p>
  
  <pre><code>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);
  
              }
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>if (value.equalsIgnoreCase("normal")) {
  
      session.setAttribute("logedin", "0");
      filterChain.doFilter(httpServletRequest, httpServletResponse);
  
  } else if (value.equalsIgnoreCase("loginUser")) {
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks</p>