Servlet App Event Listeners

Servlet App Event Listeners

by Stephanie Fesler
04/12/2001

Sun Microsystems released the proposed final draft of the Servlet 2.3 specification on October 20, 2000. Although the Servlet 2.3 specification is not in its final version yet, we do not have to wait any longer to play with the exciting new features defined by it. Apache's Tomcat has released a build, version 4.0 beta 1 that fully supports the Servlet 2.3 proposed final draft specification.

My last article was an introduction to Servlet concepts (for those new to this area) and then it was an overview of two new additions to the specification, the application lifecycle events and Servlet filters. This article is going to go into detail about writing the application lifecycle events.

Working Environment

All the code that will be shown in this article has been tested on Apache's Tomcat version 4.0 beta 1 on a Windows platform. This version of Tomcat has full support of the new Servlet 2.3 specification and the JSP 1.2 specification. If you need help installing and setting up Tomcat refer to James Goodwill's article Installing and Configuring Tomcat for an easy to follow guide on installing and configuring Apache's Tomcat version 4.0 beta 1.

All the code is part of a Web Application called demo.

Overview of Application Lifecycle Events

The Servlet 2.3 proposed final draft specification has defined application lifecycle events to provide Web Application developers more interaction with the ServletContext object and HttpSession objects. Web Application developers write event listeners so they can now be notified when lifecycle events happen (such as creation or destruction) or when attributes are modified in the ServletContext object or HttpSession objects.

Event listeners are Java classes that follow the JavaBeans design and are provided by the Web Application developer in the Web archive (.war) file. There are two types of event listeners, and both types apply to the ServletContext object and HttpSession objects. The two types are lifecycle events and changes to attributes events. Table 1 shows the types of events, a brief description, and the listener interface to implement. This table is taken from the Servlet 2.3 proposed final draft specification.

Table 1: Supported Event Types
Event Type Description Listener Interface
Servlet Context Events
Lifecycle The Servlet context has just been created and is available to service its first request, or the Servlet context is about to be shutdown. javax.Servlet.ServletContextListener
Changes to Attributes Attributes on the Servlet context has been added, removed, or replaced. javax.Servlet.ServletContextAttributesListener
Http Session Events
Lifecycle An HttpSession has just been created, or has been invalidated or timed out. javax.Servlet.http.HttpSessionListener
Changes to Attributes Attributes have been added, removed or replaced in an HttpSession object. javax.Servlet.http.HttpSessionAttributesListener

An interesting point is that there can be multiple listener classes listening to each event type and the Web application developer has the flexibility to define the order the event listener objects are invoked.

The container manages the lifecycle of event listeners. It is the containers responsibility to instantiate each of the listener classes in a Web Application before the execution of the first request into the Web Application. Also, each of the listener classes must be referenced until the Web Application services the last request.

For more details on the container's responsibilities, refer to my last article.

[1] [2] [3] [4] [5] [6] [7] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > Java Servlets > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction