29.01.2013 Views

WebSphere Application Server - IBM Redbooks

WebSphere Application Server - IBM Redbooks

WebSphere Application Server - IBM Redbooks

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Initial state<br />

Figure 7-2 A servlet’s life cycle<br />

Loading<br />

Initialization<br />

Service<br />

handling<br />

Destruction<br />

Final state<br />

All along its life, a servlet interacts closely with its container. Following the J2EE<br />

specification, the container is in charge of providing a runtime execution for the<br />

components. It is also responsible for handling a set of services such as<br />

concurrency, administration, and security.<br />

The servlet container is implied in all the steps through the servlet’s life cycle:<br />

1. Loading: The servlet container uses a classloader to locate the class file of<br />

the servlet and to instantiate it. This operation follows the Java class loading<br />

process. An instance of the servlet class (and only one) is then available at<br />

the end of this step.<br />

2. Initialization: The container activates the servlet by calling one of its<br />

methods, the init() method. The init() method must be implemented by a<br />

servlet class. This method is called once by the container and is used to<br />

initialize a set of parameters for the servlet instance. The servlet is then ready<br />

to handle requests from clients.<br />

3. Service handling: Each client request generates a call to the service() method<br />

of the servlet class. This method can either generate the response and<br />

transfer it to the Web server (via the servlet container) or dispatch the request<br />

to another component within the container.<br />

Chapter 7. Working with servlets 217

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

Saved successfully!

Ooh no, something went wrong!