ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Servlet App Event Listeners
Pages: 1, 2, 3, 4, 5, 6, 7

Deploying All Of The Above Listeners and Servlets


Listing 6: The deployment descriptor, web.xml, which contains the definitions for the listener classes and Servlet defined above.

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>

   <!-- Define application events listeners -->
  <listener>
    <listener-class>
      com.listeners.MyContextListener
    </listener-class>
  </listener>

  <listener>
    <listener-class>
      com.listeners.ServletContextAttribListener
    </listener-class>
  </listener>


  <!-- Define servlets that are included in the example application -->
  <servlet>
    <servlet-name>
      simple
    </servlet-name>
    <servlet-class>
      com.servlets.SimpleServlet
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>
      bank
    </servlet-name>
    <servlet-class>
      com.servlets.BankBalance
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>
      context attribs
    </servlet-name>
    <servlet-class>
      com.servlets.ServletContextAttrib
    </servlet-class>
  </servlet>

  <!-- Define servlet mappings to urls -->
<servlet-mapping>
    <servlet-name>
      simple
    </servlet-name>
    <url-pattern>
      /simple
    </url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>
      bank
    </servlet-name>
    <url-pattern>
      /bank
    </url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>
      context attribs
    </servlet-name>
    <url-pattern>
      /servletcontextattrib
    </url-pattern>
  </servlet-mapping>

</web-app>


Working With Session Information

The application event listeners can also make notifications when an HttpSession object is created, destroyed, or has its attributes modified. This is another way the Servlet 2.3 specification is empowering the web application programmer.

It is possible to write one application event listener that handles all notifications for changes to the HttpSession objects. This could be done with ServletContext notifications, but the earlier examples kept them separate to show that method.

To receive lifecycle notifications for HttpSession objects write a Java class that implements the javax.Servlet.http.HttpSessionListener interface. This interface defines two methods and their signatures are (taken from the JavaDocs):

void sessionCreated (HttpSessionEvent se)
  Notification that a session was created.

void sessionDestroyed (HttpSessionEvent se)
  Notification that a session was invalidated.

To receive notifications that the attributes of an HttpSession object have been modified implement the javax.servlet.http.HttpSessionAttributesListener interface. This interface defines three methods and their signatures are (taken from the JavaDocs):

void attributeAdded (HttpSessionBindingEvent se)
  Notification that an attribute has been added to a session.

void attributeRemoved (HttpSessionBindingEvent se)
  Notification that an attribute has been removed from a session.

void attributeReplaced (HttpSessionBindingEvent se)
  Notification that an attribute has been replaced in a session.

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow