aspose file tools*
The moose likes JSP and the fly likes How do you retrieve all the sessions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How do you retrieve all the sessions?" Watch "How do you retrieve all the sessions?" New topic
Author

How do you retrieve all the sessions?

Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
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

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

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.


Groovy
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
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

Joined: Feb 27, 2002
Posts: 8898

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

Joined: Jun 08, 2002
Posts: 3178
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

Joined: Nov 07, 2003
Posts: 4982
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.


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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 ]

42
brad balmer
Ranch Hand

Joined: Mar 08, 2004
Posts: 57
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);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you retrieve all the sessions?
 
Similar Threads
how to apply practically cookies /session concept
sessions
Synchronization between Sessions
Session timeout - How to set for an application?
Get session attribute values from all activated session's