Servlet Context Events HTTP Session Events Servlet Request Events Configuring an Event Listener Class

11-2 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server Use Web application event listener classes to: ■ Manage database connections when a Web application is deployed or shuts down ■ Create standard counter utilities ■ Monitor the state of HTTP sessions and their attributes

11.2 Servlet Context Events

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

11.3 HTTP Session Events

The following table lists the types of HTTP session events your event listener class must implement to respond to the HTTP session events and the methods invoked when the HTTP session events occur. Table 11–1 Servlet Context Events 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 Table 11–2 HTTP Session Events 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.HttpSessionAttribut eListener attributeAdded An attribute is removed. javax.servlet.http.HttpSessionAttribut eListener attributeRemoved An attribute is replaced. javax.servlet.http.HttpSessionAttribut eListener attributeReplaced Application Events and Event Listener Classes 11-3

11.4 Servlet Request Events

The following table lists the types of Servlet request events, the interface your event listener class must implement to manage state across the life cycle of servlet requests and the methods invoked when the request events occur.

11.5 Configuring an Event Listener Class

To configure an event listener class: 1. Open the web.xml deployment descriptor of the Web application for which you are creating an event listener class in a text editor. The web.xml file is located in the WEB-INF directory of your Web application. 2. Add an event declaration using the listener element of the web.xml deployment descriptor. The event declaration defines the event 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 event listener class for each type of event. WebLogic Server invokes the event listener classes in the order that they appear in the deployment descriptor except for shutdown events, which are invoked in the reverse order. For example: listener Note: The Servlet 2.5 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. Table 11–3 Servlet Request Events Type of Event Interface Method The request is about to go out of scope of the Web application. javax.servlet.ServletRequest Listener requestDestroyed The request is about to come into scope of the Web application. javax.servlet.ServletRequest Listener requestInitialized Notification that a new attribute was added to the servlet request. Called after the attribute is added. javax.servlet.ServletRequest AttributeListener attributeAdded Notification that a new attribute was removed from the servlet request. Called after the attribute is removed. javax.servlet.ServletRequest AttributeListener attributeRemoved Notification that an attribute was replaced on the servlet request. Called after the attribute is replaced. javax.servlet.ServletRequest AttributeListener attributeReplaced 11-4 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server listener-classmyApp.MyContextListenerClasslistener-class listener listener listener-classmyApp.MySessionAttributeListenerClasslistener-class listener 3. Write and deploy the event listener class. For details, see the section, Section 11.6, Writing an Event Listener Class .