|
<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
Lesson 14, using Listeners
What is a Listener? Implementting a ServeltContextListener Update the Tag Library Descriptor Using your new Listener 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.
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.
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. 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. Your taglib1.2.tld should now look like this. 5 Using your new Listener
We are now ready to test your new Listener. Hopefully you should see output like in the example below.
With this test we end our lesson on taking advantage of Listeners in Taglibraries. Continue with lesson 14, "Writing Validators". Copyright © 2005 IronFlare AB |