0% found this document useful (0 votes)
2K views5 pages

Java Servlet Life Cycle Explained

A servlet is a Java class that extends the capabilities of web servers. It allows servers to dynamically generate web pages in response to client requests. When a client sends a request, the servlet processes the request, generates a response, and sends it back to the client. The servlet lifecycle includes loading, initialization, request handling, and destruction. Upon loading, the servlet is initialized via the init() method. It then handles requests via the service() method and is terminated via the destroy() method.

Uploaded by

Yash Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views5 pages

Java Servlet Life Cycle Explained

A servlet is a Java class that extends the capabilities of web servers. It allows servers to dynamically generate web pages in response to client requests. When a client sends a request, the servlet processes the request, generates a response, and sends it back to the client. The servlet lifecycle includes loading, initialization, request handling, and destruction. Upon loading, the servlet is initialized via the init() method. It then handles requests via the service() method and is terminated via the destroy() method.

Uploaded by

Yash Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Servlets

A servlet is a Java Programming language class that is used to extend the capabilities of servers that
host applications accessed by means of a request-response programming model.
Although servlets can respond to any type of request, they are commonly used to extend the
applications hosted by web servers. It is also a web component that is deployed on the server to
create a dynamic web page.

request
Client Server

response sent at response generated at


client runtime

Analysis

In this figure you can see, a client sends a request to the server and the server generates the
response, analyses it and sends the response to the client.
Java Servlets Architecture
The architecture, here, discusses the communication interface, protocol used, requirements of client
and server, the programming with the languages and software involved. Basically, it performs the
below-mentioned tasks.

✓ First, it reads the explicit data sent by the clients (browsers). This data can include an HTML form
on a Web page, an applet or a custom HTTP client program. It also reads implicit HTTP request
data sent by the clients (browsers). This can include cookies, media types and compression
schemes the browser understands, and so forth.

HTTP Protocol Web Browser

HTTP Server Servlet Program Database


✓ After that, the servlet processes the data and generate the results. This process may require
communicating to a database, executing an RMI, invoking a Web service, or computing the
response directly.

✓ After processing, it sends the explicit data (i.e., the document) to the clients (browsers). This
document can be sent in a variety of formats, including text (HTML or XML), binary (GIF
images), or Excel formats.

✓ Finally, it also sends the implicit HTTP response to the clients (browsers). This includes telling
the browsers or other clients what type of document is being returned.
Servlet Life Cycle
The Servlet life cycle mainly includes the following four stages,
1) Loading a Servlet
2) Initializing the Servlet
3) Request handling
4) Destroying the Servlet

Loading &
Start
Instantiation
init( )

Initialized
End of Request
service( )
Thread
Handling
Request
destroy( )

Stop End
1. When the web server (e.g. Apache Tomcat) starts up, the servlet container deploy and loads all
the servlets.
2. The servlet is initialized by calling the init() method. The [Link]() method is called by the
Servlet container to indicate that this Servlet instance is instantiated successfully and is about
to put into service.
3. The servlet then calls service() method to process a client’s request. This method is invoked to
inform the Servlet about the client requests.
4. The servlet is terminated by calling the destroy().
5. The destroy() method runs only once during the lifetime of a Servlet and signals the end of the
Servlet instance.

init() and destroy() methods are called only once. Finally, a servlet is garbage collected by the
garbage collector of the JVM. So this concludes the life cycle of a servlet. Now, let me guide
you through the steps of creating java servlets.

You might also like