Re: Obtaining a list of all active sessions within a Servlet container.
On 14.04.2008 09:41, sd.balasubramani@gmail.com wrote:
Hi Theo,
Use HttpSessionActivationListener as follows for your requirement,
<<
class SessionCounterListener implements HttpSessionActivationListener
{
public static final Map activeSessions = HashMap<String,
HttpSession>();
public void sessionDidActivate(HttpSessionEvent event) {
HttpSession session = event.getSession();
activeSessions.put(session.getId(), session);
}
public void sessionWillPassivate(HttpSessionEvent event) {
HttpSession session = event.getSession();
activeSessions.remove(session.getId();
}
}
Define the above listener in web.xml as,
<listener>
<listener-class>my.package.SessionCounterListener</listener-class>
</listener>
Use below code to get the active sessions,
SessionCounterListener.activeSessions.size() - Returns the number of
active sessions.
SessionCounterListener.activeSessions.getValues() - Returns the all
the active sessions.
.... and don't forget proper synchronization.
:-)
robert