1

i want to get a map of all active session in my application. i am using struts and tomcat.

or

how i create a map of only active session for my application with session id as key and session object as value. Thanks

  • 1
    possible duplicate of [How can i load Java HttpSession from JSESSIONID?](http://stackoverflow.com/a/3092495/1225328) – sp00m Nov 15 '13 at 10:46

2 Answers2

1

Yes, you should definitely go for HttpSessionListener to implement. This will help you capture the session creation and destroy events. That's where you can write the logic to add and remove the session objects from map accordingly.

For example

 @Override
  public void sessionCreated(HttpSessionEvent arg0) {
    //get the session object from the event
    //put the session id and session object to your map
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent arg0) {
    //get the session object
    //remove it from the map using it's session id
  } 
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

There is no way of doing exactly that in TOMCAT. At least not a portable solution. I guess you could search the stored sessions on the filesystem if that's the manner you are storing sessions (it's up to your Tomcat's configuration).

An alternative would be registering a HttpSessionListener and counting and storing them your self.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292