public class SessionCount implements HttpSessionListener {
// The session listener model is similar to the context listener model.
// Any listener interested in observing the http session lifecycle
// should implement the HttpSessionListener interface.
// The methods accept an HttpSessionEvent instance with a getSession() method
// to return the session being created or destroyed.
private static int activeSessions = 0;
public static int getActiveSessions() {
return activeSessions;
}
public void sessionCreated(HttpSessionEvent e) {
// Called when a session is created
activeSessions++;
}
public void sessionDestroyed(HttpSessionEvent e) {
// Called when a session is destroyed (invalidated)
if(activeSessions > 0) activeSessions--;
}
}