title: Calculating inactive session time when browser is closed and reloaded in java
body: <p>Hi I have the below code in my project to check and validate the session.</p>
  <pre><code>javax.servlet.http.HttpSession httpSession = request.getSession();
  
      // If the session is new
      if (httpSession.isNew())
      {
          errorCode = 2;
          errorMessage = &quot;Invalid HTTP session&quot;;
          return false;
      }
  
      // If there is a Inactivity of 20 minutes, we expire the session on
      // server
      long now = new java.util.Date().getTime();
      long accessedTime = httpSession.getLastAccessedTime();
      if ((now - httpSession.getLastAccessedTime()) &gt;= (MAX_INACTIVE_INTERVAL * 1000L))
      {
          errorCode = 2;
          errorMessage = &quot;Inactivity of 20 minutes&quot;;
          return false;
      }
      else
      {
          httpSession.setMaxInactiveInterval((int) MAX_INACTIVE_INTERVAL);
      }
  </code></pre>
  <p>EVerything works fine. But when the user closes the browser, and opens it, the session is still active. When i debugged the code, <code>now - httpSession.getLastAccessedTime()</code> is 0. Or in other words, both are same values. How can I check the session when user closes the browser?</p>
  <p>Edit: The issue is when the browser is again loaded with the url, the lastaccessedtime also gets updated.</p>