title: Java / JSP / Sessions
body: <p>I have implemented a <code>SessionListener</code> to count sessions on my web app. I have <code>setMaxInterval(60)</code> and my problem is the following: if the session expires the session counter is set to 0 and if i start to navigate on the web page again, the session counter is not incremented. Why not?</p>
  
  <pre><code>public class SessionListener implements HttpSessionListener {
  
  static int counter = 0;
  
  public void sessionCreated(HttpSessionEvent e)
  {
      HttpSession s = e.getSession();
  
      s.setMaxInactiveInterval(60);
  
      counter++;
  
      synchronized(s.getServletContext())
      {
          s.getServletContext().setAttribute("allConnections", counter);
  
      }
  }
  
  public void sessionDestroyed(HttpSessionEvent e)
  {
      HttpSession s = e.getSession();
  
      counter--;
  
      synchronized(s.getServletContext())
      {
          s.getServletContext().setAttribute("allConnections", counter);
      }
  
  
  }
  </code></pre>
  
  <p>Then, each JSP display the counter variable on the frontend by <code>application.getAttribute("allConnections")</code></p>