package rrz;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionListener implements HttpSessionListener {
HashSet sessionSet = null;
ServletContext application = null;
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
if(application == null)application = session.getServletContext();
sessionSet = (HashSet)application.getAttribute("sessionSet");
if(sessionSet == null){
application.setAttribute("sessionSet",new HashSet());
sessionSet = (HashSet)application.getAttribute("sessionSet");
}
sessionSet.add(session.getId());
}
public void sessionDestroyed(HttpSessionEvent event) {
sessionSet.remove(event.getSession().getId());
}
}
Register it in your web app's web.xml with
rrz.SessionListener
and test it on a JSP with
<%
session.setMaxInactiveInterval(3); // for easy testing
%>
Session count is <%=((HashSet)application.getAttribute("sessionSet")).size()%>
|