Debugging with Event Listeners

 < Free Open Study > 



The event listener mechanism allows us to trap certain container events such as the creation and destruction of contexts and sessions as well as the modification of session and context attributes. This is important information that will allow us to debug problems associated with session tracking and the use of data held in the session or the context.

Implementing the Debugging Listener

We have a single event listener to implement, which will implement four interfaces:

  • ServletContextListener - through which we're notified of context creation and destruction

  • HttpSessionListener - through which we're notified of session creation and destruction

  • ServletContextAttributeListener - through which we're notified when context attributes are added, modified and removed

  • HttpSessionAttributeListener - through which we're notified when session attributes are added, modified and removed

    package debugging;    import java.io.*;    import javax.servlet.*;    import javax.servlet.http.*;    public class DebugListener implements ServletContextListener, HttpSessionListener,                                          ServletContextAttributeListener,                                          HttpSessionAttributeListener {      private ServletContext servletContext = null; 

We implement two utility methods that are used to write information to the log. By using these methods we can easily change the way in which we output our debugging information - we might, for example, want to write to a file other than the servlet log:

      private void logContext(String message) {        if (servletContext != null) {          StringBuffer messageBuffer = new StringBuffer();          messageBuffer.append("<invocation><sender>");          messageBuffer.append(servletContext.getServerInfo());          messageBuffer.append("</sender><message>" + message +                               "</message><receiver>");          String contextName = servletContext.getServletContextName();          if (contextName == null) {            contextName=servletContext.toString();          }          messageBuffer.append("CONTEXT: " + contextName);          messageBuffer.append("</receiver></invocation>");          servletContext.log(messageBuffer.toString());        }      }      private void logSession(String message, HttpSessionEvent event) {        if (servletContext != null) {          StringBuffer messageBuffer = new StringBuffer();          messageBuffer.append("<invocation><sender>");          messageBuffer.append(servletContext.getServerInfo());          messageBuffer.append("</sender><message>" + message +                               "</message><receiver>");          messageBuffer.append("SESSION: " + event.getSession().getId());          messageBuffer.append("</receiver></invocation>");          servletContext.log(messageBuffer.toString());        }      } 

We need a set of methods to record the context initialization and destruction, the session creation and destruction, and the modification of attribute values:

      public void contextInitialized(ServletContextEvent event) {        servletContext = event.getServletContext();        logContext("init()");      }      public void contextDestroyed(ServletContextEvent event) {        logContext("destroy");        servletContext = null;      }      public void attributeAdded(ServletContextAttributeEvent event) {        logContext("setAttribute(" + event.getName() + "," +                   event.getValue().toString() + ")");      }      public void attributeReplaced(ServletContextAttributeEvent event) {        logContext("setAttribute(" + event.getName() + "," +                   event.getValue().toString() + ")");      }      public void attributeRemoved(ServletContextAttributeEvent event) {        logContext("removeAttribute(" + event.getName() + ")");      }      public void sessionCreated(HttpSessionEvent event) {        logSession("create", event);      }      public void sessionDestroyed(HttpSessionEvent event) {        logSession("destroy", event);      }      public void attributeAdded(HttpSessionBindingEvent event) {        logSession("setAttribute(" + event.getName() + "," +                   event.getValue().toString() + ")", event);      }      public void attributeReplaced(HttpSessionBindingEvent event) {        logSession("setAttribute(" + event.getName() + "," +                   event.getValue().toString() + ")", event);      }      public void attributeRemoved(HttpSessionBindingEvent event) {        logSession("removeAttribute(" + event.getName() + ")", event);      } 

Using the Debugging Listener

As with the Debugging Filter, we'll test the Debugging Listener by applying it to the example servlets that come with Tomcat. Compile and copy DebugListener into %CATALINA_HOME%/webapps/examples/WEB-INF/classes/debugging. Then add the following entry to the web.xml file in %CATALINA_HOME%/webapps/examples/WEB-INF:

     <listener>       <listener-class>debugging.DebugListener</listener-class>     </listener> 

Restart Tomcat and again try an example servlet. I accessed the SessionExample servlet at http://localhost:8080/examples/servlet/SessionExample and saw the following entry in the server's log file:

     <invocation>       <sender>127.0.0.1</sender>       <message></message>       <receiver>/examples/images/code.gif</receiver>       <duration>10</duration>     </invocation>     ...     <invocation>       <sender>127.0.0.1</sender>       <message></message>       <receiver>/examples/images/return.gif</receiver>       <duration>0</duration>     </invocation>     ...     <invocation>       <sender>127.0.0.1</sender>       <message></message>       <receiver>/examples/servlet/SessionExample</receiver>       <duration>201</duration>     </invocation> 

This technique is complementary to the one we created using filters. Using both methods we can now trace:

  • The creation of a servlet context (using an event listener)

  • Each servlet invocation with its request parameters (using a filter)

  • Whether a request triggered creation of a new session (using an event listener)

  • Whether any session or context attributes were modified by the servlet (using an event listener)

  • The destruction of the session and the context (using an event listener)

For many debugging tasks, that's all that's required.



 < Free Open Study > 



Professional Java Servlets 2.3
Professional Java Servlets 2.3
ISBN: 186100561X
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net