Alex Rivera | Logout

Getting a list of active sessions in Tomcat using Java

Asked 2010-11-01T13:49:54.580
27

I am developing a project in Java in which I want the count of all active sessions in Tomcat. Based on that I want to see how much of those users are active and actually using the application.

Edit
Report

1 Answer

19

There isn't any way to get the session count directly from tomcat. But you can create and register a session listener and up the count when its created. Here is an example:

http://tomcat-configure.blogspot.com/2009/01/tomcat-session-listener-example.html

public class SessionCounter implements HttpSessionListener {

  private static int activeSessions = 0;

  public void sessionCreated(HttpSessionEvent se) {
    activeSessions++;
  }

  public void sessionDestroyed(HttpSessionEvent se) {
    if(activeSessions > 0)
      activeSessions--;
    }

  public static int getActiveSessions() {
     return activeSessions;
  }
}
answered 2010-11-01T13:58:17.010

Your Answer