title: how to differentiate a new log in (new Session) and a session time out
body: <p>Could someone let me know how to differentiate a new log in (new Session) and a session time out .<br>
  Only on login : user must be authenticated and redirected to a servlet to load user details (on other screnarios user must not be redirected to the servlet) 
  and on timeout user must be redirected to the timeout page.<br>
  To identify a new session (user not logged in):<br>
  -- Session attributes cannot be used since the session becomes null on timeout.<br>
  -- Setting cookies for session management didnt work. 
  The cookies are getting removed for the current session </p>
  
  <pre><code>Cookie cookie = new Cookie("activeSession", null);
  cookie.setMaxAge(0);
  cookie.setPath("/");
  cookie.setValue("");
  httpServletResponse.addCookie(cookie);
  getCookieValue(httpServletRequest , "activeSession"); returns null
  public static String getCookieValue(HttpServletRequest request, String name) {
      Cookie[] cookies = request.getCookies();
      if (cookies != null) {
          for (Cookie cookie : cookies) {
              if (cookie != null &amp;&amp; name.equals(cookie.getName())) {
                  return cookie.getValue();
              }
          }
      }
      return null;
  }
  </code></pre>
  
  <p>After logout or Timeout (session is invalidated) when user logs in and a new session is created.
  The cookies that were removed in the previous sessions reappears with the  preset values.  </p>
  
  <pre><code> getCookieValue(httpServletRequest , "activeSession") returns a value; 
  </code></pre>
  
  <p>If I use the below approach it works for the 1 st login attempt. 
  After the first login session has timedout ,the filter redirects to timeout page. 
  The actual problem arises when user accesses the application in the same window after timeout.   </p>
  
  <pre><code>public void doFilter(ServletRequest request, ServletResponse response,FilterChain   filterChain) throws IOException,
  {   if ((request instanceof HttpServletRequest)
              &amp;&amp; (response instanceof HttpServletResponse)) {
  HttpServletRequest httpServletRequest = (HttpServletRequest) request;
  
  HttpServletResponse httpServletResponse = (HttpServletResponse) response;
  
  //Check for a new login 
  if (httpServletRequest.getSession(false) == null  &amp;&amp; httpServletRequest.getRequestedSessionId()==null)
  {
  // Create a new session
  httpServletRequest.getSession();
  //Redirect to servlet on 1 st login to fetch details from DB
  httpRequest.getRequestDispatcher("/loginServlet").forward(request,response);
  }else{
  
  //validate active or timedout sessions. 
  boolean isSessionValid = (httpServletRequest.getRequestedSessionId() != null) &amp;&amp; !httpServletRequest.isRequestedSessionIdValid();
  
  if(isSessionValid)
  {
  httpServletResponse.sendRedirect(getTimeoutPage());
  }
  }
  }
  filterChain.doFilter(request, response);
  }
  </code></pre>
  
  <p>Therefore the details are not fetched from DB and the page is not loaded correctly.</p>
  
  <p>Browser:IE 8
  Server : Weblogic server</p>