Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get a list of all HttpSession objects in a web application?
    primarykey
    data
    text
    <p>Let's say I have a running Java-based web application with 0 or more valid <code>HttpSession</code> objects associated with it. I want a way to access the current list of valid <code>HttpSession</code> objects. I was thinking that I could implement an <code>HttpSessionListener</code> and use it to append to a list of session id values that are stored in an application-scoped attribute, but then I'm on the hook to update the list as sessions are invalidated and who knows what else.</p> <p>Before I start baking my own solution I thought I should ask the question:<br> <strong>Does the servlet API provide some means of getting access to the complete list of non-invalidated session objects?</strong></p> <p>I am using Tomcat 6.x as my web application container, and the MyFaces 1.2.x (JSF) library. </p> <p><strong>SOLUTION</strong><br> I followed an approach similar to what BalusC discussed in these existing questions:</p> <ul> <li><a href="https://stackoverflow.com/questions/3271676/how-to-easily-implement-who-is-online-in-grails-or-java-application/3271748#3271748">How to easily implement "who is online" in Grails or Java Application ?</a></li> <li><a href="https://stackoverflow.com/questions/2372311/jsf-how-to-invalidate-an-user-session-when-he-logs-twice-with-the-same-credentia/2372990#2372990">JSF: How to invalidate an user session when he logs twice with the same credentials</a></li> </ul> <p>I modified by <code>SessionData</code> class to implement <code>HttpSessionBindingListener</code>. When a binding event happens, the object will either add or remove itself from the set of all the <code>SessionData</code> objects.</p> <pre><code>@Override public void valueBound(HttpSessionBindingEvent event) { // Get my custom application-scoped attribute ApplicationData applicationData = getApplicationData(); // Get the set of all SessionData objects and add myself to it Set&lt;SessionData&gt; activeSessions = applicationData.getActiveSessions(); if (!activeSessions.contains(this)) { activeSessions.add(this); } } @Override public void valueUnbound(HttpSessionBindingEvent event) { HttpSession session = event.getSession(); ApplicationData applicationData = getApplicationData(); Set&lt;SessionData&gt; activeSessions = applicationData.getActiveSessions(); if (activeSessions.contains(this)) { activeSessions.remove(this); } } </code></pre> <p>The one thing that continues to irritate me is what happens when Tomcat is restarted. Unless Tomcat has been properly configured to NOT serialize sessions to disk, it will do so. When Tomcat starts up again, the <code>HttpSession</code> objects (and the <code>SessionData</code> objects along with them) are deserialized and the sessions are made valid again. However, the serialization/deserialization totally sidesteps the <code>HttpSession</code> listener events, so I do not have the opportunity to gracefully put the deserialized reference to the <code>SessionData</code> back in my managed Set of objects after the restart.</p> <p>I don't have any control over the production configuration of Tomcat in my customer's organization, so I cannot assume that it will be done the way I expect it.</p> <p>My workaround is to compare the <code>HttpSession</code> creation time with the application startup time when a request is received. If the session was created before the application startup time, then I call <code>invalidate()</code> and the user is sent to an error/warning page with an explanation of what happened.</p> <p>I get the application startup time by implementing a <code>ServletContextListener</code> and storing the current time inside an application-scoped object from within the <code>contextInitialized()</code> method of my listener.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload