Lifecycle of a Web Application

 < 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:

  • contextInitialized(ServletContextEvent e)

  • contextDestroyed(ServletContextEvent e)

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:

click to expand

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:

  • attributeAdded(ServletContextAttributeEvent e)

  • attributeReplaced(ServletContextAttributeEvent e)

  • attributeRemoved(ServletContextAttributeEvent e)

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:

click to expand

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 > 



Professional Java Servlets 2.3
Professional Java Servlets 2.3
ISBN: 186100561X
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net