- Monitoring Creation and Destruction of the Servlet Context
- Example: Initializing Commonly Used Data
- Detecting Changes in Servlet Context Attributes
- Example: Monitoring Changes to Commonly Used Data
- Packaging Listeners with Tag Libraries
- Example: Packaging the Company Name Listeners
- Recognizing Session Creation and Destruction
- Example: A Listener That Counts Sessions
- Watching for Changes in Session Attributes
- Example: Monitoring Yacht Orders
- Using Multiple Cooperating Listeners
- The Complete Events Deployment Descriptor
10.3 Detecting Changes in Servlet Context Attributes
OK, when the Web application is loaded, you can set up initial values of resources and store references to them in the servlet context. But what if you want to be notified whenever these resources change? For example, what if the value of resource B depends on the value of resource A? If resource A changes, you need to automatically update the value of resource B. Handling this situation is the job of servlet context attribute listeners. Using them involves the following steps.
Implement the ServletContextAttributeListenerinter-face. This interface is in the javax.servletpackage.
Override attributeAdded, attributeReplaced, and attributeRemoved. The attributeAddedmethod is triggered when a new attribute is added to the servlet context. When a new value is assigned to an existing servlet context attribute, attribute-Addedis triggered with the new value and attributeReplacedis triggered with the old value (i.e., the value being replaced). The attributeRemovedmethod is triggered when a servlet context attribute is removed altogether.
Obtain references to the attribute name, attribute value, and servlet context. Each of the three ServletContextAttribute-Listenermethods takes a ServletContextAttributeEventas an argument. The ServletContextAttributeEventclass has three useful methods: getName (the name of the attribute that was changed), getValue (the value of the changed attributethe new value for attributeAdded and the previous value for attribute-Replaced and attributeRemoved), and getServletContext (the servlet context).
Use the objects. You normally compare the attribute name to a stored name to see if it is the one you are monitoring. The attribute value is used in an application-specific manner. The servlet context is usually used to read previously stored attributes (getAttribute), store new or changed attributes (setAttribute), and make entries in the log file (log).
Declare the listener. Use the listenerand listener-class elements to simply list the fully qualified name of the listener class, as below.
<listener> <listener-class>somePackage.SomeListener</listener-class> </listener>
For now, assume that this declaration goes in the web.xml file immediately before any servlet elements. However, in Section 10.5 you'll see that if you package listeners with tag libraries, you can use the identical declaration within the TLD (tag library descriptor) file of the tag library.
The following section gives an example.