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

Description

Servlet context: lifecycle event

javax.servlet.ServletContextListener

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

javax.servlet.ServletContextAttributeListener

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

HTTP session: lifecycle event

javax.servlet.http.HttpSessionListener

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

HTTP session: attribute changes

javax.servlet.http.HttpSessionAttributeListener

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 {
         _connection.close();
      } 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.

         Class.forName(jdbcDriver).newInstance();
         Connection conn =
            DriverManager.getConnection(dbURL,user,password);
         _connection = conn;
         _context.setAttribute("DBConnection", conn);

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