bea.com | products | dev2dev | support | askBEA

Download Docs | Site Map | Glossary
Search

Assembling and Configuring Web Applications

Application Events and Listeners

The following sections describe how to configure and use Web Application events and listeners:

- Overview of Application Events and Listeners

- Servlet Context Events

- HTTP Session Events

- Configuring an Event Listener

- Writing a Listener Class

- Templates for Listener Classes

- Additional Resources

Overview of Application Events and Listeners

Application events provide notifications of a change in state of the servlet context (each Web Application uses its own servlet context) or of an HTTP session object. You write event listener classes that respond to these changes in state and you configure and deploy Application event and listener classes in a Web Application.

For servlet context events, the event listener classes can receive notification when the Web Application is deployed or is being undeployed (or when WebLogic Server shuts down), and when attributes are added, removed, or replaced.

For HTTP session events, the event listener classes can receive notification when an HTTP session is activated or is about to be passivated, and when an HTTP session attribute is added, removed, or replaced.

Use Web Application events to:

- Manage database connections when a Web Application is deployed or shuts down

- Create counters

- Monitor the state of HTTP sessions and their attributes

Servlet Context Events

The following table lists the types of Servlet context events, the interface your event listener class must implement to respond to the event, and the methods invoked when the event occurs.

Type of Event | Interface | Method
--- | --- | ---
Servlet context is created. | javax.servlet.ServletContextListener | contextInitialized()
Servlet context is about to be shut down. | javax.servlet.ServletContextListener | contextDestroyed()
An attribute is added. | javax.servlet. ServletContextAttributesListener | attributeAdded()
An attribute is removed. | javax.servlet. ServletContextAttributesListener | attributeRemoved()
An attribute is replaced. | javax.servlet. ServletContextAttributesListener | attributeReplaced()

HTTP Session Events

The following table lists the types of HTTP session events, the interface your event listener class must implement to respond to the event, and the methods invoked when the event occurs.

Type of Event | Interface | Method
--- | --- | ---
An HTTP session is activated. | javax.servlet.http. HttpSessionListener | sessionCreated()
An HTTP session is about to be passivated. | javax.servlet.http. HttpSessionListener | sessionDestroyed()
An attribute is added. | javax.servlet.http. HttpSessionAttributeListener | attributeAdded()
An attribute is removed. | javax.servlet.http. HttpSessionAttributeListener | attributeRemoved()
An attribute is replaced. | javax.servlet.http. HttpSessionAttributeListener | attributeReplaced()

Note: The Servlet 2.3 specification also contains the javax.servlet.http.HttpSessionBindingListener and the javax.servlet.http.HttpSessionActivationListener interfaces. These interfaces are implemented by objects that are stored as session attributes and do not require registration of an event listener in web.xml. For more information, see the Javadocs for these interfaces.

Configuring an Event Listener

To configure an event listener:

1. Open the web.xml deployment descriptor of the Web Application for which you are creating an event listener in a text editor, or use the Web Application Deployment Descriptor Editor (see Web Application Deployment Descriptor Editor Help.) The web.xml file is located in the WEB-INF directory of your Web Application.

2. Add an event declaration using the <listener> element. The event declaration defines the listener class that is invoked when the event occurs. The <listener> element must directly follow the <filter> and <filter-mapping> elements and directly precede the <servlet> element. You can specify more than one listener class for each type of event. WebLogic Server invokes the event listeners in the order that they appear in the deployment descriptor (except for shutdown events, which are invoked in the reverse order). For example:

   >     <listener>
   >       <listener-class>myApp.myContextListenerClass</listener-class>
   >     </listener>

   >     <listener>
   >       <listener-class>myApp.mySessionAttributeListenerClass</listener-class>
   >     </listener>

3. Write and deploy the Listener class. See the next section, Writing a Listener Class, for details.

Writing a Listener Class

To write a listener class:

1. Create a new class that implements the appropriate interface for the type of event your class responds to. For a list of these interfaces, see Servlet Context Events or HTTP Session Events See Templates for Listener Classes for sample templates you can use to get started.

2. Create a public constructor that takes no arguments.

3. Implement the required methods of the interface. See the J2EE API Reference (Javadocs) for more information.

4. Copy the compiled event listener classes into the WEB-INF/classes directory of the Web Application, or package them into a jar file and copy the jar file into the WEB-INF/lib directory of the Web Application.

The following useful classes are passed into the listener methods in a listener class:

  javax.servlet.http.HttpSessionEvent

  provides access to the HTTP session object

  javax.servlet.ServletContextEvent

  provides access to the servlet context object.

  javax.servlet.ServletContextAttributeEvent

  provides access to servlet context and its attributes

  javax.servlet.http.HttpSessionBindingEvent

  provides access to an HTTP session and its attributes

Templates for Listener Classes

The following examples provide some basic templates for listener classes.

Servlet Context Listener Example

>     package myApp;
>     import javax.servlet.*;

>     public final class myContextListenerClass implements
>        ServletContextListener {
>         public void contextInitialized(ServletContextEvent event) {

>           /* This method is called when the servlet context is
>              initialized(when the Web Application is deployed).
>              You can initialize servlet context related data here.
>           */

>         }

>         public void contextDestroyed(ServletContextEvent event) {

>           /* This method is invoked when the Servlet Context
>              (the Web Application) is undeployed or
>              WebLogic Server shuts down.
>           */

>         }
>     }

HTTP Session Attribute Listener Example

>     package myApp;
>     import javax.servlet.*;

>     public final class mySessionAttributeListenerClass implements
>        HttpSessionAttributeListener {

>         public void attributeAdded(HttpSessionBindingEvent sbe) {
>           /* This method is called when an attribute
>              is added to a session.
>           */
>         }

>         public void attributeRemoved(HttpSessionBindingEvent sbe) {
>           /* This method is called when an attribute
>              is removed from a session.
>           */
>         }

>         public void attributeReplaced(HttpSessionBindingEvent sbe) {
>           /* This method is invoked when an attibute
>              is replaced in a session.
>           */
>         }
>     }

Additional Resources

- Assembling and Configuring Web Applications

- Writing Web Application Deployment Descriptors

- Servlet 2.3 Specification from Sun Microsystems

- J2EE API Reference (Javadocs)

- The J2EE Tutorial from Sun Microsystems: