title: Tomcat persistent and user specific session counter
body: <p>I need to make persistent and user specific session counter. I made this</p>
  
  <pre><code>package my.package;
  
  import javax.servlet.http.HttpSessionListener;
  import javax.servlet.http.HttpSessionEvent;
  import javax.servlet.http.HttpSession;
  import java.util.HashMap;
  
  public class SessionCounter implements HttpSessionListener {
  
      private static HashMap activeSessions;
  
      public SessionCounter() {
          //How to restore session count?
          activeSessions = new HashMap();
      }
  
      public void sessionCreated(HttpSessionEvent se) {
          HttpSession session = se.getSession();
          String userName = session.getAttribute("username");
          Integer count = (Integer) activeSessions.get(userName);
          if (count != null) {
              activeSessions.put(userName, Integer.valueOf(count.intValue() + 1));
          } else {
              activeSessions.put(userName, new Integer(1));
          }
      }
  
      public void sessionDestroyed(HttpSessionEvent se) {
          HttpSession session = se.getSession();
          String userName = session.getAttribute("username");
          Integer count = (Integer) activeSessions.get(userName);
          if (count != null &amp;&amp; count.intValue() &gt; 0) {
              activeSessions.put(userName, Integer.valueOf(count.intValue() - 1));
          }
  
      }
  
      public static HashMap getActiveSessions() {
          return activeSessions;
      }
  }
  </code></pre>
  
  <p>Session are active even after restart tomcat but session count stored in my activeSessions variable is lost. How can I restore the session count after restart?</p>