<taglib:tutorial lesson="13">

In this lesson, we will take a closer look at the listener directive that was added to the Taglibrary descriptor in JSP 1.2

1 Lesson 14, using Listeners

    In this lesson, we will learn how our Taglibraries can benefit from the addition of the listener-directive that was added in JSP 1.2. After briefly going through the basic of Listeners and how these can be a valuable asset to our taglibraries we will write a sample ServletContextListener to exemplify the implementation of the various Listener interfaces.

2 What is a Listener?

    The listener directive was added to the Taglibrary descriptor in JSP 1.2 and works just as a listener-directive in a Web-module descriptor (web.xml file).

    The container automatially instantiates and registers any listeners described in the Taglibrary descriptor. Depending on the type of Listerner, the methods of the implementing class will be called when the events it listens to are fired.


    Figure 1. There are Listeners for most needs

    Listeners are usually used from with Web-applications where they are used to keep track of values being bound to sessions, session creations and such. For example, a ServletContextListener could be used to make sure that unique (within this instance of the Web-module) resources are added to the ServletContext when the Web-module is started (the Container will fire its contextInitialized() method) and removed when the Web-module is stopped (the Container will fire its contextDestroyed() method). Think of this as a pattern for Web-module singleton-like behaviour. Your Tags can rely on that certain resources are always loaded before the Tag is invoked. For example a StatelessSessionBean that are heavily used by the Tags are added to the ServletContext and then utilized without lookups or narrowing within the individual tags.

    Other Listeners can be used to determine when a request comes in, when a value is bound/unbound from the session or request scope, and so on.

    Lets take a look at what a Listener implementation might look like and how it is configured within the Taglibrary descriptor

3 Implementting a ServeltContextListener

    The sample Listener we will now write will implement the ServletContextListener interface, which will lead to that our implementation is notified when the ServletContext is initialized and destroyed.


    Figure 12 Our ServletContextListener will implement the ServletContextListener interface

    As this is only a sample Listener it wont do anything fancier than notifying us (through console output) when the Web-module is initialized or destroyed.

    1. In the '/WEB-INF/classes/com/acme/listener/' directory, create a new class called 'SimpleContextListener.java' with the following content:


      package com.acme.listener;

      import javax.servlet.ServletContextListener;
      import javax.servlet.ServletContextEvent;

      public class SimpleContextListener implements ServletContextListener
      {
      Listing 1: Class declaration.

      Above we declare that our class, with the name SimpleContextListener, implements the ServletContextListener interface.

    2. Add the following ServletContextListener method:


      public void contextInitialized(ServletContextEvent event)
      {
      System.out.println("context initialized");
      }
      Listing 2: Adding the contextInitialized() method.

      The method above is defined in the ServletContextLlistener interface and will be called by the Container when the ServletContext is initialized. All our implementation of this method will do is to write a message to the console when the event is fired.

    3. Add the last ServletContextListener method:


      public void contextDestroyed(ServletContextEvent event)
      {
      System.out.println("context destroyed");
      }
      }
      Listing 3: Adding the contextDestroyed() method and ending the class.

      The method above is defined in the ServletContextLlistener interface and will be called by the Container when the ServletContext is destroyed. All our implementation of this method will do is to write a message to the console when the event is fired.

    If you've typed this in correctly, your SimpleContextListener.java should look like this.

4 Update the Tag Library Descriptor

    We now need to tell our JSP Container about our new Listener.

    1. Open your 'taglib1.2.tld' from your '/WEB-INF/' directory with your favourite editor.

    2. Add the listener-class tag to the taglibrary descriptor so that it looks like in listing 4 below.


      <taglib>
      <tlib-version>1.0</tlib-version>
      <jsp-version>1.2</jsp-version>
      <short-name>mt2</short-name>
      <uri>http://www.orionserver.com/tutorials/mytags2</uri>
      <description>My Second Tag library</description>
      <listener>
      <listener-class>com.acme.tag.listener.SimpleContextListener</listener-class>
      </listener>
      Listing 4: Adding the listener-class tag to the descriptor.

    Your taglib1.2.tld should now look like this.

5 Using your new Listener

    We are now ready to test your new Listener.

    1. Stop your Orion Application Server.

    2. Clear the console output.

    3. Open the URL http://localhost/taglib/errorTag.jsp (or any other URL of our Web-application) in a normal web browser.

    Hopefully you should see output like in the example below.


    Orion/3.0 initialized
    context initialized
    Listing 5: Sample console output

    With this test we end our lesson on taking advantage of Listeners in Taglibraries.

    Continue with lesson 14, "Writing Validators".

Copyright © 2005 IronFlare AB