aspose file tools*
The moose likes Servlets and the fly likes how to count active sessions in Servlet 2.2 specification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to count active sessions in Servlet 2.2 specification" Watch "how to count active sessions in Servlet 2.2 specification" New topic
Author

how to count active sessions in Servlet 2.2 specification

Sarah Jones
Greenhorn

Joined: Jul 19, 2004
Posts: 12
HI
I have to count no of active sessions
We are using Servlet 2.2 and IPlanet6.0
In servlet 2.3 one way is to implement HttpSessionListener.
How to do the same in Servlet 2.2.
Vicky Jain
Ranch Hand

Joined: Aug 02, 2003
Posts: 52
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);

Thanks


SCJP1.4(96%)<br />A drop of ink can make millions think.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
Vicky Jain
Ranch Hand

Joined: Aug 02, 2003
Posts: 52
Hi Jeroen,

Thanks for poiting it out. But can u please tell me that in what scenarion some session may not be properly invalidated and why?

Cheers..!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to count active sessions in Servlet 2.2 specification