10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Servlet Life Cycle<br />

Servlet Life Cycle<br />

102<br />

Troubleshooting Duke's Bookstore Database<br />

Problems<br />

<strong>The</strong> Duke’s Bookstore database access object returns the following exceptions:<br />

■ BookNotFoundException: Returned if a book can’t be located in the bookstore database.<br />

This will occur if you haven’t loaded the bookstore database with data or the server has not<br />

been started or has crashed. You can populate the database by running ant create-tables.<br />

■ BooksNotFoundException: Returned if the bookstore data can’t be retrieved. This will occur<br />

if you haven’t loaded the bookstore database with data or if the database server hasn’t been<br />

started or it has crashed.<br />

■ UnavailableException: Returned if a servlet can’t retrieve the web context attribute<br />

representing the bookstore. This will occur if the database server hasn’t been started.<br />

Because you have specified an error page, you will see the message<br />

<strong>The</strong> application is unavailable. Please try later.<br />

If you don’t specify an error page, the web container generates a default page containing the<br />

message<br />

A Servlet Exception Has Occurred<br />

and a stack trace that can help you diagnose the cause of the exception. If you use<br />

errorpage.html, you will have to look in the server log to determine the cause of the exception.<br />

<strong>The</strong> life cycle of a servlet is controlled by the container in which the servlet has been deployed.<br />

When a request is mapped to a servlet, the container performs the following steps.<br />

1. If an instance of the servlet does not exist, the web container<br />

a. Loads the servlet class.<br />

b. Creates an instance of the servlet class.<br />

c. Initializes the servlet instance by calling the init method. Initialization is covered in<br />

“Initializing a Servlet” on page 109.<br />

2. Invokes the service method, passing request and response objects. Service methods are<br />

discussed in “Writing Service Methods” on page 110.<br />

If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s<br />

destroy method. Finalization is discussed in “Finalizing a Servlet” on page 128.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!