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
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.
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
posted
0
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...
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.
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 ]
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);