• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to count active sessions in Servlet 2.2 specification

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..!
 
reply
    Bookmark Topic Watch Topic
  • New Topic