ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Servlet App Event Listeners
Pages: 1, 2, 3, 4, 5, 6, 7

Figure 1 shows the console for Tomcat when it starts up and the above web application is deployed.

Screen shot.
Figure 1. Tomcat's console when it starts up.

Figure 2 is a snapshot of the directory structure to give you a good idea of how the demo web application is set up.

Screen shot.
Figure 2. A snapshot of Tomcat's working directory.

Listening For Changes To The Servlet Context

The Servlet 2.3 specification allows for more interaction between the web application programmer and ServletContext. The programmer can now write an application lifecycle event listener that executes when the attributes of the ServletContext object have been modified. If you want to execute some code when the attributes of the ServletContext object has been modified, write a Java class that implements the javax.Servlet.ServletContextAttributesListener interface. This interface defines three methods with the following signatures (this is taken from the JavaDocs):

void attributeAdded (ServletContextAttributeEvent scab)
  Notification that a new attribute was added to the servlet context.

void attributeRemoved (ServletContextAttributeEvent scab)
  Notification that an existing attribute has been removed from the servlet context.

void attributeReplaced (ServletContextAttributeEvent scab)
  Notification that an attribute on the servlet context has been replaced.

Related Reading

Java Servlet Programming, 2nd EditionJava Servlet Programming, 2nd Edition
By Jason Hunter with William Crawford
Table of Contents
Sample Chapter
Full Description

Listing 3 is an example of writing an application event listener for getting notifications of changes to the ServletContext object.

Listing 3: An application event listener that writes messages to the console when attributes of the ServletContext object have been modified. (ServletContextAttribListener.java)

package com.listeners;

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

public class ServletContextAttribListener
    implements ServletContextAttributesListener {

  //This method is invoked when an attribute
  //is added to the ServletContext object
  public void attributeAdded (ServletContextAttributeEvent scab)
    System.out.println("An attribute was added to the " +
      "ServletContext object");

  //This method is invoked when an attribute
  //is removed from the ServletContext object
  public void attributeRemoved (ServletContextAttributeEvent scab)
    System.out.println("An attribute was removed from " +
      "the ServletContext object");

  //This method is invoked when an attribute
  //is replaced in the ServletContext object
  public void attributeReplaced (ServletContextAttributeEvent scab)
    System.out.println("An attribute was replaced in the " +
      "ServletContext object");


This code is well and good, but what about testing it? Listing 4 is for an HTML document that allows the user to add name-value pairs as an object to the ServletContext object. You can also remove items and replace items in the ServletContext object. Listing 5 is the code for the Servlet that parses the request from the HTML document and actually does the manipulation of the ServletContext object.

Listing 4: An HTML document that allows users to play with the ServletContext object. (servletcontextattrib.html)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<title>Working With Attributes Of the ServletContext Object</title>

<font size="6" color="navy">
  Playing With The ServletContext Object

<form name="MyForm" action="./servletcontextattrib" method="POST">
<table width="75%" bgcolor="silver" align="center">

  <tr><td rowspan="3">Select an Action</td>
    <td><input type="radio" name="action" value="add">
      Add Item To ServletContext
    <td><input type="radio" name="action" value="remove">
      Remove Item From ServletContext
    <td><input type="radio" name="action" value="replace">
      Replace Item In ServletContext

  <tr><td>Servlet Context Attribute Name</td>
    <td><input type="text" name="name" value=""></td>
  <tr><td>Servlet Context Attribute Value</td>
    <td><input type="text" name="value" value=""></td>

  <tr><td colspan="2" align="center">
    <input type="submit" name="btnSubmit" value="Submit">



Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow