tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Hauser <>
Subject HttpSessionListener: Negative session count
Date Tue, 12 Aug 2003 08:21:23 GMT
Hello J2EE programmers

I wrote a JSP some time ago that uses a class SessionCounter (which 
implements HttpSessionListener) to count the active sessions of a web 
application and to display them.

Unfortunately there seems to be a problem with my program, because the 
web application says that there are currently -18 active sessions. 
However, the lowest possible count should be 0.

Of course I could implement the sessionDestroyed the following way (to 
prohibit a negative session count, but then the current session count 
would not be correct, not?
     public void sessionDestroyed(HttpSessionEvent event) {
       if (activeSessions > 0)

Code of JSP file:
     <td>Concurrent sessions:</td>
     <td><%= SessionCounter.getActiveSessions() %></td>
     <td>Last refresh:</td>
     <td><%= new Date() %></td>
     <td>Last session change:</td>
     <td><%= SessionCounter.getLastChange() %></td>

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

   public class SessionCounter implements HttpSessionListener {

     /** Static variable to keep track of the current number of active 
sessions. */
     private static int activeSessions = 0;

     public void sessionCreated(HttpSessionEvent event) {

     public void sessionDestroyed(HttpSessionEvent event) {

     public static int getActiveSessions() {
       return activeSessions;

I'm using Tomcat 4.1.24 and hope that someone of you might give me a 
hint on how I can get a reliable session counter.


View raw message