Count number of users accessing web application
For any web application sometimes we need to track active user accessing the application. One way to keep track of active users using HttpSessionListener and increase or decrease counter based on when session created or destroyed.Below is example class which shows how to track active users:
package com.javahonk; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class ActiveSessionCounter implements HttpSessionListener { private static int activeUser = 0; @Override public void sessionCreated(HttpSessionEvent arg0) { activeUser++; } @Override public void sessionDestroyed(HttpSessionEvent arg0) { if(activeUser > 0){ activeUser--; } } }