10.1. Monitoring Creation and Destruction of the Servlet Context

The ServletContextListener class responds to the initialization and destruction of the servlet context. These events correspond to the creation and shutdown of the Web application itself. The ServletContextListener is most commonly used to set up application-wide resources like database connection pools and to read the initial values of application-wide data that will be used by multiple servlets and JSP pages. Using the listener involves the following six steps.

1.
Implement the ServletContextListener interface. This interface is in the javax.servlet package.

2.
Override contextInitialized and contextDestroyed. The first of these (contextInitialized) is triggered when the Web application is first loaded and the servlet context is created. The two most common tasks performed by this method are creating application-wide data (often by reading context initialization parameters) and storing that data in an easily accessible location (often in attributes of the servlet context). The second method (contextDestroyed) is triggered when the Web application is being shut down and the servlet context is about to be destroyed. The most common task performed by this method is the releasing of resources. For example, contextDestroyed can be used to close database connections associated with a now-obsolete connection pool. However, since the servlet context will be destroyed (and garbage collected if the server itself continues to execute), there is no need to use contextDestroyed to remove normal objects from servlet context attributes.

3.
Obtain a reference to the servlet context. The contextInitialized and contextDestroyed methods each take a ServletContextEvent as an argument. The ServletContextEvent class has a getServletContext method that returns the servlet context.

4.
Use the servlet context. You read initialization parameters with getInitParameter, store data with setAttribute, and make log file entries with log.

5.
Declare the listener. Use the listener and 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 the servlet element). 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.

6.
Provide any needed initialization parameters. Once you have a reference to the servlet context (see Step 3), you can use the getInitParameter method to read context initialization parameters as the basis of data that will be made available to all servlets and JSP pages. You use the context-param web.xml element to provide the names and values of these initialization parameters, as follows.

<context-param> 
  <param-name>name</param-name> 
  <param-value>value</param-value> 
</context-param> 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.124