File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Counting active sessions of web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Counting active sessions of web application" Watch "Counting active sessions of web application" New topic
Author

Counting active sessions of web application

Christian Hauser
Greenhorn

Joined: Feb 17, 2003
Posts: 14
Hello
I wrote a JSP and a Servlet some time ago that both count the active sessions of a web application and display them.
To do that I wrote a class SessionCounter which implements HttpSessionListener and thus is being informed when a new session is created or a session has been destroyed.
Unfortunately there seems to be a problem with my program, because the web application says that there are currently -16 active sessions.
Maybe there is a problem within my code or some resources within the servlet engine (ServletExec 4.1) are not reset when restarted.
I hope someone can give me a hint.
Thank you in advance,
Christian
Code of JSP (similar in Servlets in other web applications):

Here's the class SessionCounter, which belongs to a utility JAR that is situated in the lib directory of each web application.
Ryan Lowe
Greenhorn

Joined: Aug 04, 2003
Posts: 18
In your sessionDestroyed...
public void sessionDestroyed ( HttpSessionEvent se )
{
if ( activeSessions > 0 )
{
activeSessions--;
}
}
Your code continues to decrement even if activeSessions is a negative number.
onlyOOD
Christian Hauser
Greenhorn

Joined: Feb 17, 2003
Posts: 14
Okey, but why is it that I get negative numbers? Is this a problem of my ServletEngine or does it have to be this way?
Christian (a little confused about HttpSessionEvents)
Rangarajan Suresh
Ranch Hand

Joined: Jan 08, 2002
Posts: 50
Originally posted by Christian Hauser:
Okey, but why is it that I get negative numbers? Is this a problem of my ServletEngine or does it have to be this way?
Christian (a little confused about HttpSessionEvents)

what is the scope of the bean?
I remember tomcat having problems in invalidating sessions.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
JAMon is a performance tuning API, but it also does much more than that. Here is some sample code using JAMon that I have successfully used to count http sessions. You can also use it to co unt the number of times any particular user logs in as well as if they are currently logged in. You can also tell maximum and average number of sessions and more. JAMon can also track hits, average max and min execution times per page and more.
Sample session and page hit tracking code can be found at
http://www.javaperformancetuning.com/tools/jamon/#SampleCode
JAMon documentation and the download can be found at http://www.jamonapi.com
Steve


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Christian Hauser
Greenhorn

Joined: Feb 17, 2003
Posts: 14
Thank you Steve for pointing me to your JAMon API. I'll have a look at it.
However, I still do not understand WHY it is possible that I get a negative session count. Why should there be more sessionDestroyed() calls than sessionCreated() (overall)?
Christian (still confused).
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

For the benefit of those not on the tomcat-user list...

Tomcat will restore an active session after restart, but your static variable will not. So if you have 20 active sessions and the server bounces, your sessions are re-created without the sessionCreated event being called.

When one of these sessions eventually gets destroyed, your counter will go negative.
Christian Hauser
Greenhorn

Joined: Feb 17, 2003
Posts: 14
Thank you Mike.
In fact, I was just about to start updating this thread when I read your post.
Christian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Counting active sessions of web application
 
Similar Threads
HttpSessionListener
setMaxInactiveInterval
jsp Sessions
Some errors with my ActiveSessions counter
Count number of users accesing the web application