Feb. 21, 2007, 10:02 a.m.
posted by reo
Servlet Life CycleThe 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 396). 1 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
Defining The Listener ClassYou define a listener class as an implementation of a listener interface. Figure 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 helper 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.BookDB;
import javax.servlet.*;
import util.Counter;
public final class ContextListener
implements ServletContextListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
try {
BookDB bookDB = new BookDB();
context.setAttribute("bookDB", bookDB);
} catch (Exception ex) {
System.out.println(
"Couldn't create database: "
+ ex.getMessage());
}
Counter counter = new Counter();
context.setAttribute("hitCounter", counter);
context.log("Created hitCounter"
+ counter.getCounter());
counter = new Counter();
context.setAttribute("orderCounter", counter);
context.log("Created orderCounter"
+ counter.getCounter());
}
public void contextDestroyed(ServletContextEvent event) {
context = event.getServletContext();
BookDB bookDB = context.getAttribute(
"bookDB");
bookDB.remove();
context.removeAttribute("bookDB");
context.removeAttribute("hitCounter");
context.removeAttribute("orderCounter");
}
}
Specifying Event Listener ClassesTo specify an event listener class, you add a listener element to the Web application deployment descriptor. Here is the listener element for the Duke's Bookstore application:
<listener>
<listener-class>listeners.ContextListener</listener-class>
</listener>
2 Handling ErrorsAny number of exceptions can occur when a servlet is executed. The Web container will generate a default page containing the message A Servlet Exception Has Occurredwhen an exception occurs, but you can also specify that the container should return a specific error page for a given exception. To specify such a page, you add an error-page element to the Web application deployment descriptor. These elements map the exceptions returned by the Duke's Bookstore application to errorpage.html:
<error-page>
<exception-type>exception.BookNotFoundException</exception-type>
<location>/errorpage.html</location>
</error-page>
<error-page>
<exception-type>exception.BooksNotFoundException</exception-
type>
<location>/errorpage.html</location>
</error-page>
<error-page>
<exception-type>exception.OrderException</exception-type>
<location>/errorpage.html</location>
</error-page>
|
- Comment