I have the following listner to count the active sessions.
---------------------------------------------------------
package com.cs;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionCounter implements HttpSessionListener {
privatestatic int activeSessions =0;
/**
* @see javax.servlet.http.HttpSessionListener#void (javax.servlet.http.HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent arg0) {
activeSessions++;
System.out.println(activeSessions + " SESSIONS ARE CREATED");
}
/**
* @see javax.servlet.http.HttpSessionListener#void (javax.servlet.http.HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent arg0) {
if (activeSessions>0){
activeSessions--;
System.out.println(activeSessions + " SESSIONS ARE DISTROYED");
}
}
public static int getActiveSessions() {
System.out.println("THE NUMBER OF ACTIVE SESSIONS IS : " + activeSessions);
return activeSessions;
}
}
-------------------------------------------------------
I have the following tester class to
test the listner.
------------------------------------------------------
package com.cs;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* @version 1.0
* @author
*/
public class TestSessionCounter extends HttpServlet implements
Servlet {
/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//SessionCounter s = new SessionCounter();
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("The number of sessions Active is: " + SessionCounter.getActiveSessions());
}
/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
------------------------------------------------------
Whenever I open a new session, it counts it. But when I close the browser, it doesn�t decrement the count. Anybody know why? Any help greatly appreciated.
Thanks in advance.