0% found this document useful (0 votes)
9 views12 pages

Servlet

The document outlines the life cycle of a Servlet, which is managed by the Servlet container through four main stages: loading, initializing, request handling, and destroying. It details the methods involved, including init(), service(), and destroy(), explaining their roles in managing resources and handling client requests. Example code is provided to illustrate the implementation of these methods in a Java Servlet.

Uploaded by

Jack Sparrow
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)
9 views12 pages

Servlet

The document outlines the life cycle of a Servlet, which is managed by the Servlet container through four main stages: loading, initializing, request handling, and destroying. It details the methods involved, including init(), service(), and destroy(), explaining their roles in managing resources and handling client requests. Example code is provided to illustrate the implementation of these methods in a Java Servlet.

Uploaded by

Jack Sparrow
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
You are on page 1/ 12

BY-

Iynesh Karthick M-2212041


Aswin K-2212010
The entire life cycle of a Servlet is managed by the Servlet container which uses
the javax.servlet.Servlet Interface to understand the Servlet object and manage it. So, before creating
a Servlet object, let’s first understand the life cycle of the Servlet object which is actually
understanding how the Servlet container manages the Servlet object.
The Servlet life cycle mainly goes through four stages,

 Loading a Servlet.

 Initializing the Servlet.

 Request handling.

 Destroying the Servlet.


Loading a Servlet
The first stage of the Servlet lifecycle involves loading and initializing the Servlet by the Servlet
container. The Web container or Servlet Container can load the Servlet at either of the following two
stages

1. Initializing the context, on configuring the Servlet with a zero or positive integer value.

2. If the Servlet is not preceding stage, it may delay the loading process until the Web container
determines that this Servlet is needed to service a request.

The Servlet container performs two operations in this stage :


•Loading : Loads the Servlet class.

•Instantiation : Creates an instance of the Servlet. To create a new instance of the Servlet, the
container uses the no-argument constructor.
2.Initialializing a Servlet
After the Servlet is instantiated successfully, the Servlet container initializes the instantiated Servlet object. The container initializes the
Servlet object by invoking the Servlet.init(ServletConfig) method which accepts ServletConfig object reference as parameter.The
Servlet container invokes the Servlet.init(ServletConfig) method only once, immediately after the Servlet.init(ServletConfig) object is
instantiated successfully. This method is used to initialize the resources, such as JDBC datasource.
• Now, if the Servlet fails to initialize, then it informs the Servlet container by throwing
the ServletException or UnavailableException.
3.Handling request

After initialization, the Servlet instance is ready to serve the client requests. The Servlet container performs the following operations
when the Servlet instance is located to service a request :

• It creates the ServletRequest and ServletResponse objects. In this case, if this is a HTTP request, then the Web container
creates HttpServletRequest and HttpServletResponse objects which are subtypes of
the ServletRequest and ServletResponse objects respectively.

• After creating the request and response objects it invokes the Servlet.service(ServletRequest, ServletResponse) method by
passing the request and response objects.
Destroying a Servlet

• When a Servlet container decides to destroy the Servlet, it performs the following operations,It allows all
the threads currently running in the service method of the Servlet instance to complete their jobs and get
released.

• After currently running threads have completed their jobs, the Servlet container calls
the destroy() method on the Servlet instance.

 After the destroy() method is executed, the Servlet container releases all the references of this Servlet
instance so that it becomes eligible for garbage collection.
Servlet Life Cycle Methods
There are three life cycle methods of a Servlet :
•init()

•service()

•destroy()
init() method:
• This method is called only once to load the servlet.Since it is called only once in it’s
lifetime,therefore “connected architecture” code is written inside it because we only want once
to get connected with the database.

• Init()Method accept only one parameter.

• Init() Method is Executed by Webserver,WebContainers,and Servlet Containers.

Prototype of init() method:


public void init() throws ServletException {
// Initialization code...
}

Example:
public void init() throws ServletException {
// Initialization code
System.out.println("Servlet Initialized");
}
Service() method:

• Service method is mainly used to write the processing Logic.


• It runs depend on the number of request from client.
• The service() method is the most important method to perform that provides the connection between client and server.
• The web server calls the service() method to handle requests coming from the client( web browsers) and to send response
back to the client.
• Webcontainer executes the service method.

The prototype for this method:


public void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException { }

Request:
Request parameter are useful inorder to retrive the input data from the html form.
Response:
Response object is mainly useful inorder to provide response to the client based upon the request.

Example code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("Hello from Servlet!");
}
Destroy() method:
• The destroy() method is called only once.

• It is called at the end of the life cycle of the servlet.

• This method performs various tasks such as closing connection with the database, releasing memory allocated to
the servlet, releasing resources that are allocated to the servlet and other cleanup activities.

• When this method is called, the garbage collector comes into action.

Prototype for Response():

public void destroy() {


// Finalization code...
}

Example code:
public void destroy() {
// Cleanup code
System.out.println("Servlet Destroyed");
}
Example Code:
// Java program to show servlet example
// Importing required Java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class AdvanceJavaConcepts extends HttpServlet
{
private String output;

// Initializing servlet
public void init() throws ServletException
{
output = "Advance Java Concepts";
}

// Requesting and printing the output


public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(output);
}
//destroy the servlet
public void destroy()
{
System.out.println("Over");
}
}
Thank You

You might also like