You are on page 1of 5

Application life cycle listeners and events

With application life cycle listeners and events, which are now part of the Serv
let API, you can notify interested listeners when servlet contexts and sessions
change. For example, you 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 int
eractions with ServletContext and HttpSession objects. Servlet context listeners
manage resources at an application level. Session listeners manage resources th
at are associated with a series of requests from a single client. Listeners are
available for life cycle events and for attribute modification events. The liste
ner developer creates a class that implements the javax listener interface, corr
esponding to the listener functionality that you want.
At application startup time, the container uses introspection to create an insta
nce of your listener class and registers it with the appropriate event generator
.
When a servlet context is created, the contextInitialized method of your listene
r class is invoked, which creates the database connection for the servlets in yo
ur application to use if this context is for your application. All servlet conte
xt listeners are notified of context initialization before any servlet in the We
b application is initialized.
When the servlet context is destroyed, your contextDestroyed method is invoked,
which releases the database connection, if this context is for your application.
You must destroy all servlets before any servlet context listeners are notified
of context destruction.
Notifications to session listeners precedes notifications to context listeners.
Listener classes for servlet context and session changes
The following methods are defined as part of the javax.servlet.ServletContextLis
tener interface:
void contextInitialized(ServletContextEvent)
Notification that the Web application is ready to process requests. Place code i
n this method to see if the created context is for your Web application and if i
t is, allocate a database connection and store the connection in the servlet con
text.
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 your Web application and if it is, c
lose the database connection stored in the servlet context.
The following methods are defined as part of the javax.servlet.ServletRequestLis
tener 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 fi
lter 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 packa
ge:
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
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)
{
}
}

/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package listeners;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* Example listener for context-related application events, which were
* introduced in the 2.3 version of the Servlet API. This listener
* merely do*****ents the occurrence of such events in the application log
* associated with our servlet context.
*
* @author Craig R. McClanahan
* @version $Revision: 267129 $ $Date: 2004-03-18 11:40:35 -0500 (Thu, 18 Mar 20
04) $
*/
public final class ContextListener
implements ServletContextAttributeListener, ServletContextListener {

// ----------------------------------------------------- Instance Variables

/**
* The servlet context with which we are associated.
*/
private ServletContext context = null;

// --------------------------------------------------------- Public Methods

/**
* Record the fact that a servlet context attribute was added.
*
* @param event The servlet context attribute event
*/
public void attributeAdded(ServletContextAttributeEvent event) {
log("attributeAdded('" + event.getName() + "', '" +
event.getValue() + "')");
}

/**
* Record the fact that a servlet context attribute was removed.
*
* @param event The servlet context attribute event
*/
public void attributeRemoved(ServletContextAttributeEvent event) {
log("attributeRemoved('" + event.getName() + "', '" +
event.getValue() + "')");
}

/**
* Record the fact that a servlet context attribute was replaced.
*
* @param event The servlet context attribute event
*/
public void attributeReplaced(ServletContextAttributeEvent event) {
log("attributeReplaced('" + event.getName() + "', '" +
event.getValue() + "')");
}

/**
* Record the fact that this web application has been destroyed.
*
* @param event The servlet context event
*/
public void contextDestroyed(ServletContextEvent event) {
log("contextDestroyed()");
this.context = null;
}

/**
* Record the fact that this web application has been initialized.
*
* @param event The servlet context event
*/
public void contextInitialized(ServletContextEvent event) {
this.context = event.getServletContext();
log("contextInitialized()");
}

// -------------------------------------------------------- Private Methods

/**
* Log a message to the servlet context application log.
*
* @param message Message to be logged
*/
private void log(String message) {
if (context != null)
context.log("ContextListener: " + message);
else
System.out.println("ContextListener: " + message);
}
/**
* Log a message and associated exception to the servlet context
* application log.
*
* @param message Message to be logged
* @param throwable Exception to be logged
*/
private void log(String message, Throwable throwable) {
if (context != null)
context.log("ContextListener: " + message, throwable);
else {
System.out.println("ContextListener: " + message);
throwable.printStackTrace(System.out);
}
}