That got me going; thanks!
One more question:
Is there any way of telling if the session was actively invalidated, or if it timed out? Looking at the docs for HttpSessionBindingEvent, I don't see any differentiation between them. That's not a big deal, but would be nice to have.

Dave

Tim Lucia wrote:

Add the following fragment to your web.xml:

 <!-- Session Counting Filter                                           -->
 <!-- Every request passing through this filter will be checked for a   -->
 <!-- newly-created session, which will be then counted against the     -->
 <!-- total sessions using this application.                            -->
 <filter>
   <filter-name>SessionCountFilter</filter-name>
   <filter-class>SessionCountFilter</filter-class>
 </filter>
 <filter-mapping>
   <filter-name>SessionCountFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
-----Original Message-----
From: David Kerber [mailto:[EMAIL PROTECTED] Sent: Thursday, February 09, 2006 10:56 AM
To: Tomcat Users List
Subject: Re: Logging session timeouts

I got your code in, and it compiles, but I don't understand how I configure
the url-mapping you refer to.  Could you point me to some docs for that?  I
looked through the web.xml files (both the server one, and the one for the
app), but couldn't find anything about url-mapping or filters that seemed to
apply to this.  It may be there, but I don't know enough about it to
recognize it.

Thanks!
Dave


Tim Lucia wrote:

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]








---------------------------------------------------------------------
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]






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to