Servlet filters  Chapter 24: Creating JavaServer Pages

Chapter 23: Using Filters and Event Listeners

Application lifecycle event listeners

EAServer’s implementation of application lifecycle events enables you to register event listeners that can respond to state changes in a Web application’s ServletContext and HttpSession objects. When a Web application starts up, EAServer instantiates the listeners that are declared in the deployment descriptor. The servlet API provides four listener interfaces, which EAServer calls when each event occurs.

Event type

Listener interface


Servlet context: lifecycle event


The servlet context was just created and is available to service its first request, or the servlet context is about to be shut down.

Servlet context: attribute changes


Servlet context attributes have been added, removed, or replaced.

HTTP session: lifecycle event


An HttpSession has just been created, invalidated, or timed out.

HTTP session: attribute changes


HttpSession attributes have been added, removed, or replaced.

NoteThe interfaces javax.servlet.ServletContextAttributeListener and javax.servlet.http.HttpSessionAttributeListener are both new for EAServer version 4.1. The corresponding interfaces from EAServer version 4.0, where “Attributes” was plural, are not supported in EAServer 4.1.

If you need your code to remain compatible with EAServer 4.0 or other servers that require the older interface names, implement both the old and new interfaces.

“Listener properties” describes how to add a listener to a Web application.

Sample listener

Here is an example of how a ServletContextListener could be used to maintain a database connection for each servlet context. The database connection that gets created is stored in the ServletContext object as an attribute, so it is available to all the servlets in the Web application.

package listeners;

import javax.servlet.*;
import java.sql.*;

public final class ContextListener implements ServletContextListener 
   ServletContext _context = null;
   Connection _connection = null;

   * This method gets invoked when the ServletContext has
   * been destroyed. It cleans up the database connection.
   public void contextDestroyed(ServletContextEvent event) 
      // Destroy the database connection for this context.
      _context.setAttribute("DBConnection", null);
      _context = null;

      try {
      } catch (SQLException e) {
      // ignore the exception

   * This method is invoked after the ServletContext has
   * been created.  It creates a database connection.
   public void contextInitialized(ServletContextEvent event) 
      _context = event.getServletContext();
      String jdbcDriver="com.sybase.jdbc2.jdbc.SybDriver";
      String dbURL="jdbc:sybase:Tds:localhost:2638";
      String user="dba";
      String password="";

      try {
         // Create a connection and store it in the ServletContext
         // as an attribute of type Connection.

         Connection conn =
         _connection = conn;
         _context.setAttribute("DBConnection", conn);

      } catch (Exception e) {
         // Unable to create the connection, set it to null.
         _connection = null;
         _context.setAttribute("DBConnection", null);

Copyright © 2005. Sybase Inc. All rights reserved. Chapter 24: Creating JavaServer Pages