The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method. Finalization is discussed in Finalizing a Servlet (page 92). Handling Servlet Life-Cycle EventsYou can monitor and react to events in a servlet's life cycle by defining listener objects whose methods get invoked when life-cycle events occur. To use these listener objects you must define and specify the listener class. Defining the Listener ClassYou define a listener class as an implementation of a listener interface. Table 32 lists the events that can be monitored and the corresponding interface that must be implemented. When a listener method is invoked, it is passed an event that contains information appropriate to the event. For example, the methods in the HttpSessionListener interface are passed an HttpSessionEvent, which contains an HttpSession.
The listeners.ContextListener class creates and removes the database access and counter objects used in the Duke's Bookstore application. The methods retrieve the web context object from ServletContextEvent and then store (and remove) the objects as servlet context attributes. import database.BookDBAO; import javax.servlet.*; import util.Counter; import javax.ejb.*; import javax.persistence.*; public final class ContextListener implements ServletContextListener { private ServletContext context = null; @PersistenceUnit EntityManagerFactory emf; public void contextInitialized(ServletContextEvent event) { context = event.getServletContext(); try { BookDBAO bookDB = new BookDBAO(emf); context.setAttribute("bookDB", bookDB); } catch (Exception ex) { System.out.println( "Couldn't create database: " + ex.getMessage()); } Counter counter = new Counter(); context.setAttribute("hitCounter", counter); counter = new Counter(); context.setAttribute("orderCounter", counter); } public void contextDestroyed(ServletContextEvent event) { context = event.getServletContext(); BookDBAO bookDB = context.getAttribute("bookDB"); bookDB.remove(); context.removeAttribute("bookDB"); context.removeAttribute("hitCounter"); context.removeAttribute("orderCounter"); } } Specifying Event Listener ClassesYou specify an event listener class using the listener element of the deployment descriptor. Review The Example Servlets (page 60) for information on how to specify the ContextListener listener class. You can specify an event listener using the deployment descriptor editor of NetBeans 5.5 by doing the following:
Handling ErrorsAny number of exceptions can occur when a servlet executes. When an exception occurs, the web container generates a default page containing the message A Servlet Exception Has Occurred But you can also specify that the container should return a specific error page for a given exception. Review the deployment descriptor file included with the example to learn how to map the exceptions exception.BookNotFound, exception.BooksNotFound, and exception.OrderException returned by the Duke's Bookstore application to errorpage.html. See Mapping Errors to Error Screens (page 51) for instructions on how to specify error pages using NetBeans 5.5. |