background image

Servlet Life Cycle

<< Deploy and Run the Application using Ant | Handling Servlet Life-Cycle Events >>
<< Deploy and Run the Application using Ant | Handling Servlet Life-Cycle Events >>

Servlet Life Cycle

Troubleshooting Duke's Bookstore Database
Problems
The Duke's Bookstore database access object returns the following exceptions:
BookNotFoundException
: Returned if a book can't be located in the bookstore database.
This will occur if you haven't loaded the bookstore database with data or the server has not
been started or has crashed. You can populate the database by running ant create-tables.
BooksNotFoundException
: Returned if the bookstore data can't be retrieved. This will occur
if you haven't loaded the bookstore database with data or if the database server hasn't been
started or it has crashed.
UnavailableException
: Returned if a servlet can't retrieve the web context attribute
representing the bookstore. This will occur if the database server hasn't been started.
Because you have specified an error page, you will see the message
The application is unavailable. Please try later.
If you don't specify an error page, the web container generates a default page containing the
message
A Servlet Exception Has Occurred
and a stack trace that can help you diagnose the cause of the exception. If you use
errorpage.html
, you will have to look in the server log to determine the cause of the exception.
Servlet 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.
1. If an instance of the servlet does not exist, the web container
a. Loads the servlet class.
b. Creates an instance of the servlet class.
c. Initializes the servlet instance by calling the init method. Initialization is covered in
"Initializing a Servlet" on page 109
.
2. Invokes the service method, passing request and response objects. Service methods are
discussed in
"Writing Service Methods" on page 110
.
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" on page 128
.
Servlet Life Cycle
The Java EE 5 Tutorial · September 2007
102