Using Jigsaw, W3C's Server, I have to find out how many sessions are there at a given time. Any help will be greatly appreciated

Jonathan Downey

The way I do this uses the 2.2 spec of the Servlet API (which is what most JSP engines support). If you create an object that implements javax.servlet.http.HttpSessionBindingListener, you can define valueBound and valueUnbound methods. I usually call it a User class. Also you can have a static int, counter, in this class, which is accessible by all classes (make it public or create increment, decrement and getValue methods). In the valueBound method, get it to increment the static int, in valueUnbound call decrement).

Now in the JSP pages, at the top, check if the User object exists in the Session scope. If it doesn't, create one and add it to the Session. This will automatically call the valueBound on that object and increment the counter. When a persons session expires, or the User object is removed from the session, the valueUnbound is automatically called and the counter decrements.

I usually make this check a seperate JSP page which I include on all the other pages. Then all you need to do is get the User.counter value at any time to see how many people are on the system.

The only problem with this is that it takes 30 mins of inactivity for a session to expire, but most web servers allow you to change this value.

If you need some code examples, email me privately and I'll send you them.