Below is a filter which keeps track of how many sessions are attached to a web app. The key part is the HttpSessionBindingListener interface.
Tim /** * J2EE "Filter" to count page hits. What it counts depends on the url-mapping * in web.xml. * * @author tim.lucia */ public class SessionCountFilter implements Filter, HttpSessionBindingListener, Serializable { private final static Log logger = LogFactory.getLog(SessionCountFilter.class); public static final Hashtable sessions = new Hashtable(); public static int sessionCountHighWater = 0; /** * Container startup notification */ public void init(FilterConfig arg0) throws ServletException { logger.debug("init(): " + arg0); } /** * Container shutdown notification */ public void destroy() { logger.debug("destroy()"); } /** * Process the container's filter request. * @param request - Request object * @param response - response object * @param chain - next filter in the chain. */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request, response); HttpServletRequest httpRequest = (HttpServletRequest)request; HttpSession session = httpRequest.getSession(false); if (logger.isDebugEnabled()) { logger.debug("Request " + httpRequest.getRequestURI() + (session == null ? " returned no session" : " belongs to session ID " + session.getId())); } // Bind to the session, if there is one, and it is new: if (null != session && session.isNew()) { session.setAttribute(toString(), this); } } /** * Implement HttpSessionBindingListener#valueBound */ public void valueBound(HttpSessionBindingEvent bindEvent) { HttpSession session = bindEvent.getSession(); final String sessionID = session.getId(); sessions.put(session, sessionID); if (logger.isDebugEnabled()) { logger.debug("[" + sessions.size() + "] CREATE: " + sessionID); } sessionCountHighWater = (sessionCountHighWater < sessions.size() ? sessions.size() : sessionCountHighWater); } /** * Implement HttpSessionBindingListener#valueUnbound */ public void valueUnbound(HttpSessionBindingEvent bindEvent) { HttpSession session = bindEvent.getSession(); final String sessionID = (String)sessions.get(session); sessions.remove(session); if (logger.isDebugEnabled()) { logger.debug("[" + sessions.size() + "] DESTROY: " + sessionID); } } /** * Return current count of sessions * @return The number of sessions currently tracked */ public static int getSessionCount() { return sessions.size(); } /** * Return high water mark of number of sessions * @return The high water mark of sessions tracked */ public static int getSessionCountHighWater() { return sessionCountHighWater; } /** * Return string representation of this object * @return a String representation of this object */ public String toString() { return getClass().getName() + "#" + hashCode(); } } -----Original Message----- From: David Kerber [mailto:[EMAIL PROTECTED] Sent: Thursday, February 09, 2006 9:38 AM To: Tomcat Users List Subject: Logging session timeouts Is there any way of trapping session timeouts, so I can log them? I am logging when a user logs in and when they explicitly log out, but would like to log when their session times out, if that is possible. TIA! Dave --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]