Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you retrieve all the sessions?

 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
What I want to do is to get a list (or array or whatever) of all sessions currently present in my app...
How would I do that?
Thanks in advance!
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's a job of App Server... Why would you need all the sessions for? If just to know the number of sessions in a web app, you may use HttpSessionListener to handle it...
There are other alternative ways to get the number of sessions as well... But we need to know what specification that you require...
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other alternative ways to get the number of sessions as well

Yes, one way that I can think of is to have a static field in HttpSessionListener. Increment the count in sessionCreated and decrement the count in sessionDestroyed.
 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I access that HttpSessionListener?
I am not looking for the number of sessions I am looking for the list of session objects themselves...
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ivan Jouikov:
How do I access that HttpSessionListener?
I am not looking for the number of sessions I am looking for the list of session objects themselves...

It may possible to get the active sessions using JMX. I am not sure though.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ivan Jouikov:
How do I access that HttpSessionListener?
I am not looking for the number of sessions I am looking for the list of session objects themselves...

Create a class that implements that HttpSessionListener and set with full path in the listener element in web.xml... Then it will be listening to the event whenever a session is started... The counter of the sessions will be increased as Pradeep mentioned...
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to access the HttpSessionListener, its' methods will be called by the Application server, when a new session is established, and an existing session is destroyed.
Simply performing adding in sessionCreated(HttpSessionEvent se) method, and subtracting in sessionDestroyed(HttpSessionEvent se) method.
Hope this help.
Nick.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You USED to be able to get all HttpSessions using HttpSessionContext, but this has been deprecated.
It's still there though in J2EE 1.3.1, but I don't know if it still does anything useful (it might just return null or some other default value when requesting a session).

but as I said this might no longer yield the expected results (and in J2EE 1.4 might not even compile. I don't have that installed here so can't check if the HttpSessionContext has been removed or not).
[ March 10, 2004: Message edited by: Jeroen Wenting ]
 
brad balmer
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you 'may' be looking for something like this which will put all of the valid HttpSession objects into a HashMap and place that object into a HashMap to be retrieved out of the servlet context?
public class SessionHolder implements HttpSessionListener {
private ServletContext context = null;
private HashMap sessions = new HashMap();

public HashMap getSessionHolder() {
return sessions;
}
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
sessions.put(session.getId(), session);

if(context == null)
storeInServletContext(event);
}
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
sessions.remove(session.getId());
}
private void storeInServletContext(HttpSessionEvent event) {
HttpSession session = event.getSession();
context = session.getServletContext();
context.setAttribute("sessionHolder", this);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic