- Objectives
- Introduction
- Overriding HttpServlet GET, POST, and PUT Methods
- Triggering HttpServlet GET, POST, and PUT Methods
- Interfacing with HTML Requests
- Web Application Scope
- Servlet Life-cycle
- Using a RequestDispatcher
- Web Application Context
- Context Within a Distributable Web Application
- Chapter Summary
- Apply Your Knowledge
Web Application Context
3.1 Identify the uses for and the interfaces (or classes) and methods to achieve the following features:
- Servlet context initialization parameters.
- Servlet context listener.
- Servlet context attribute listener.
- Session attribute listeners.
Please see the section "Interfacing with HTML Requests," earlier in this chapter, where the related objective 1.3 "Retrieve a servlet initialization parameter" is discussed. Listing 4.8 is especially helpful here because it demonstrates how to enumerate the context initialization parameter list.
Regarding listeners, you can monitor and react to servlet events by defining listener objects. These objects have methods that the container invokes when life-cycle events occur. To make this happen, you define a listener class by implementing a listener interface. The container will invoke the listener method and pass it information (methods in the HttpSessionListener interface are passed an HttpSessionEvent) about that event.
Listing 4.18 demonstrates how you could use the initialization and destruction events.
Listing 4.18 Listening for a Context Initialization and Destruction
import java.util.Date; import javax.servlet.ServletContext; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public final class ContextListener implements ServletContextListener { public void contextInitialized( ServletContextEvent event) { ServletContext context = event.getServletContext(); context.setAttribute("StartDate", Date); } public void contextDestroyed(ServletContextEvent event) { ServletContext context = event.getServletContext(); Date startDate = context.getAttribute("StartDate"); customLog(startDate); context.removeAttribute("StartDate"); } }
The attribute StartDate is set when the container initializes the application. Then when the application quits, the same attribute is logged and then deleted. For an excellent article that provides an overview of application life-cycle events, please see Servlet App Event Listeners by Stephanie Fesler (04/12/2001, http://www.onjava.com/pub/a/onjava/2001/04/12/listeners.html). The four interfaces that you can expect to see on the exam are these:
-
When a servlet is initialized or destroyed:
-
javax.servlet.ServletContextListener.
-
contextDestroyed(ServletContextEvent sce) Notification that the servlet context is about to be shut down.
-
contextInitialized(ServletContextEvent sce) Notification that the Web application is ready to process requests.
-
When a context attribute is added, removed, or replaced:
-
javax.servlet.ServletContextAttributeListener.
-
attributeAdded(ServletContextAttributeEvent scab) Notification that a new attribute was added to the servlet context.
-
attributeRemoved(ServletContextAttributeEvent scab) Notification that an existing attribute has been removed from the servlet context.
-
attributeReplaced(ServletContextAttributeEvent scab) Notification that an attribute on the servlet context has been replaced.
-
When a session is initialized or destroyed:
-
javax.servlet.http.HttpSessionListener.
-
sessionCreated(HttpSessionEvent se) Notification that a session was created.
-
sessionDestroyed(HttpSessionEvent se) Notification that a session became invalid or timed out.
-
When a session attribute is added, removed, or replaced:
-
HttpSessionAttributeListener.
-
attributeAdded(HttpSessionBindingEvent se) Notification that an attribute has been added to a session.
-
attributeRemoved(HttpSessionBindingEvent se) Notification that an attribute has been removed from a session.
- attributeReplaced(HttpSessionBindingEvent se) Notification that an attribute has been replaced in a session.