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

Discussion in 'Java' started by theog, Apr 14, 2008.

  1. theog

    theog Guest

    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
     
    theog, Apr 14, 2008
    #1
    1. Advertising

  2. theog

    Guest

    Re: Obtaining a list of all active sessions within a Servletcontainer.

    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/
     
    , Apr 14, 2008
    #2
    1. Advertising

  3. Re: Obtaining a list of all active sessions within a Servlet container.

    On 14.04.2008 09:41, 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
     
    Robert Klemme, Apr 14, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dustin Aleksiuk

    Can I get all the sessions currently active?

    Dustin Aleksiuk, Nov 10, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    600
    John Christensen
    Nov 11, 2003
  2. Christina N
    Replies:
    2
    Views:
    345
    Christina N
    Sep 15, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    2,012
    Jack Jia
    Apr 4, 2004
  4. Atul

    Getting All Active Web Sessions

    Atul, Aug 21, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    5,654
    Tim_Mac
    Aug 22, 2006
  5. puzzlecracker
    Replies:
    8
    Views:
    301
Loading...

Share This Page