Obtaining a list of all active sessions within a Servlet container.

T

theog

Hi,

I would like to obtain a list of sessions. Old way was to look at
HttpSessionContext, however that is deprecated.

Would be great if someone can share an example of how I would
accomplish this!

Cheers,
Theo
 
S

sd.balasubramani

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.

Regards,
Bala.

http://rest-client.googlecode.com/
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top