|< Free Open Study >|| |
At this point, it is important to know about the major events in the lifecycle of a typical web application.
In version 2.3 of the servlet specification, several application lifecycle events have been defined. This represents perhaps the most significant change to the Servlet API. These events provide a web application developer with more possibilities for interaction with the ServletContext object, as well as the HttpSession object. It is now possible for an application developer to write event listeners so that they can be notified when a certain lifecycle event occurs (an example event is application creation, or application destruction), or even when attributes in the ServletContext (discussed in more detail later) are modified.
We looked at event listeners in Chapter 2. For this chapter, just recall that the container is responsible for notifying a particular Java class when one of the lifecycle events occurs. The lifecycle of a web application can be broken down into two distinct parts. The first part concerns creation and destruction of the application. The events in this first part are represented by two methods:
These methods are defined by the ServletContextListener interface, and should be implemented by classes that are interested in being notified when the application is initialized or destroyed. These objects register with the container at initialization time (through the deployment descriptor) and are notified by the container when the events occur. The diagram below illustrates the sequence of events that occur:
Here we can see that when the application first starts up, the contextInitialised() method is invoked on all objects that have registered themselves as ServletContextListeners. The application then goes about its business, serving requests, and providing responses until it is shut down. Once it is shut down the contextDestroyed() method is invoked on all of the objects that have registered themselves as listeners.
The events in the second part of the application's lifecycle are events that are fired when parameters are set, changed or removed in the application (actually in the ServletContext). The three events that occur are represented by the following methods:
Classes that are interested in being notified when a parameter is added, changed, or removed in the application should implement these methods, which are defined by the ServletContextAttributeListener interface. These objects register with the container at initialization time (through the deployment descriptor) and are notified by the container when the events occur. The diagram below illustrates the sequence of events that occur:
Here we can see that when a servlet adds an attribute to the ServletContext, the attributeAdded() method is invoked on all objects that have been registered as ServletContextAttributeListeners. When a servlet changes an existing attribute in the ServletContext, the attributeReplaced() method is invoked on all of the listeners. Finally, when a servlet removes an attribute from the context the attributeRemoved() method is invoked on the listeners.
|< Free Open Study >|| |