Application life cycle listeners and events
With application life cycle listeners and events, which are now part of the Servlet API, we can notify interested listeners when servlet contexts and sessions change. For example, we can notify users when attributes change and if sessions or servlet contexts are created or destroyed.
The life cycle listeners give the application developer greater control over interactions with ServletContext and HttpSession objects. Servlet context listeners manage resources at an application level. Session listeners manage resources associated with a series of requests from a single client. Listeners are available for life cycle events and for attribute modification events. The listener developer creates a class that implements the javax listener interface, corresponding to the listener functionality that we want.
At application startup time, the container uses introspection to create an instance of our listener class and registers it with the appropriate event generator.
When a servlet context is created, the contextInitialized method of our listener class is invoked, which creates the database connection for the servlets in the application to use if this context is for the application. All servlet context listeners are notified of context initialization before any servlet in the web application is initialized.
When the servlet context is destroyed, your contextDestroyed method is invoked, which releases the database connection, if this context is for the application. We must destroy all servlets before any servlet context listeners are notified of context destruction.
Notifications to session listeners precede notifications to context listeners.
Listener classes for servlet context and session changes
The following methods are defined as part of the javax.servlet.ServletContextListener interface:
- void contextInitialized(ServletContextEvent)
Notification that the web application is ready to process requests. Place code in this method to see if the created context is for our web application and if it is, allocate a database connection and store the connection in the servlet context.
- void contextDestroyed(ServletContextEvent)
Notification that the servlet context is about to shut down. Place code in this method to see if the created context is for our web application and if it is, close the database connection stored in the servlet context.
The following methods are defined as part of the javax.servlet.ServletRequestListener interface:
- public void requestInitialized(ServletRequestEvent re)
- Notification that the request is about to come into scope
A request is defined as coming into scope when it is about to enter the first filter in the filter chain that processes the request.
- public void requestDestroyed(ServletRequestEvent re)
- Notification that the request is about to go out of scope
A request is defined as going out of scope when it exits the last filter in its filter chain.
The following listener interfaces are defined as part of the javax.servlet package:
- ServletContextListener
- ServletContextAttributeListener
The following filter interface is defined as part of the javax.servlet package:
- FilterChain interface - methods: doFilter()
The following event classes are defined as part of the javax.servlet package:
- ServletContextEvent
- ServletContextAttributeEvent
The following interfaces are defined as part of the javax.servlet.http package:
- HttpSessionListener
- HttpSessionAttributeListener
- HttpSessionActivationListener
The following event class is defined as part of the javax.servlet.http package:
- HttpSessionEvent
Example: Creating a servlet context listener with com.ibm.websphere.DBConnectionListener.java
The following example shows how to create a servlet context listener:
package com.ibm.websphere; import java.io.*; import javax.servlet.*; public class DBConnectionListener implements ServletContextListener { // implement the required context init method void contextInitialized(ServletContextEvent sce) { } // implement the required context destroy method void contextDestroyed(ServletContextEvent sce) { } }
Web applications: Resources for learning