File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to count active sessions in Servlet 2.2 specification

 
Sarah Jones
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vicky Jain
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic