How do I get a list of all HttpSession objects in a web application?
Solution 1:
No, the Servlet API doesn't provide a way. You really have to get hold of them all with help of a HttpSessionListener
. You can find several examples in the following answers:
- How to find
HttpSession
by jsessionid? - How to find number of active sessions per IP?
- How to check Who's Online?
- How to invalidate another session when a user is logged in twice?
Solution 2:
There is no straight forward way. It depends on deployment. Above will fail once you decide to introduce distributed deployment and load balancing.
Solution 3:
Not really an answer, but in the good ol' days there was "javax.servlet.http.HttpSessionContext", but it was dropped as of version 2.1, explicitly with no replacement: https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpSessionContext.html