Servlet App Event Listeners

Servlet App Event Listeners

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.

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"
  "DTD/xhtml1-strict.dtd">

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

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

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

<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>
  </tr>
  <tr>
    <td><input type="radio" name="action" value="remove">
      Remove Item From ServletContext
    </td>
  </tr>
  <tr>
    <td><input type="radio" name="action" value="replace">
      Replace Item In ServletContext
    </td>
  </tr>

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

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

</body>
</html>


h

Prev  [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