I think you can do this with the following code snippet:
// Increment the hit count for this page. The value is saved // in this client's session under the name "tracker.count". Integer count = (Integer)session.getValue("tracker.count"); if (count == null) count = new Integer(1); else count = new Integer(count.intValue() + 1); session.putValue("tracker.count", count);
SCJP1.4(96%)<br />A drop of ink can make millions think.
that won't work. It will count the number of requests in a single session
You'll need a static int in a servlet that is guaranteed to be called by each session, plus an object in the session itself that is used as a trigger (if it's there don't increase the int, if it isn't there increase the int and put the object in the session).
Make sure you decrease the int again when you invalidate the session.
This is NOT guaranteed to give the actual number of sessions of course. Most sessions will never be properly invalidated therefore the counter will not be decreased for them.
There is no way around this.
Joined: Aug 02, 2003
Thanks for poiting it out. But can u please tell me that in what scenarion some session may not be properly invalidated and why?
subject: how to count active sessions in Servlet 2.2 specification