FAQ
History |
Previous Home Next |
Search
Feedback |
DividerServlet Life Cycle
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 an instance of the servlet does not exist, the Web container
- Loads the servlet class.
- Creates an instance of the servlet class.
- Initializes the servlet instance by calling the
init
method. Initialization is covered in Initializing a Servlet.- Invokes the
service
method, passing a request and response object. Service methods are discussed in Writing Service Methods.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.Handling Servlet Life Cycle Events
You 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 listener objects you
- Define the listener class
- Specify the listener class in the Web application deployment descriptor
Defining The Listener Class
You define a listener class as an implementation of a listener interface. Servlet Life Cycle Events 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 anHttpSessionEvent
, which contains anHttpSession
.
Table 3-2 Servlet Life Cycle Events Object Event Listener Interface and Event Class Web context
(See Accessing the Web Context) Initialization and destruction Attribute added, removed, or replaced Session
(See Maintaining Client State) Creation, invalidation, and timeout Attribute added, removed, or replacedThe
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 fromServletContextEvent
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"); } }Creating a Listener
To create a listener in the IDE:
- Mount the Web module as a filesystem.
- Expand the module and
WEB-INF
nodes.- Right-click the
classes
node and choose NewRight ArrowJSP & ServletRight ArrowListenersRight ArrowXXX
Listener, whereXXX
is the type of the listener.- Type a name for the listener.
- Click Finish.
Specifying Event Listener Classes
To specify an event listener in Web application deployment descriptor using
t
he IDE:
- Mount the Web module as a filesystem.
- Expand the
WEB-INF
node.- Select the
web.xml
file.- Select the Deployment tab in the property editor.
- Select the Listeners property and open the property editor.
- Click Add.
- Type the listener class.
- Click OK twice.
For an example listener definition, see The Example Servlets.
Handling Errors
Any 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 Occurred
when 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 specify an Error Pages property for the Web application deployment descriptor (see Error Mappings).
FAQ
History |
Previous Home Next |
Search
Feedback |
All of the material in The J2EE Tutorial for the Sun ONE Platform is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.