+1 vote
in Programming Languages & Algorithms by

1 Answer

+1 vote
selected by (user.guest)
Best answer
Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But Servlets offer several advantages in comparison with the CGI.

Performance is significantly better.

Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.

Servlets are platform-independent because they are written in Java.

Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.

The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.

Java program for lifecycle of servlet:-

import java.io.*;  

import javax.servlet.*;  


public class First implements Servlet{  

ServletConfig config=null;  


public void init(ServletConfig config){  


System.out.println("servlet is initialized");  



public void service(ServletRequest req,ServletResponse res)  

throws IOException,ServletException{  




PrintWriter out=res.getWriter();  


out.print("<b>hello simple servlet</b>");  




public void destroy(){System.out.println("servlet is destroyed");}  

public ServletConfig getServletConfig(){return config;}  

public String getServletInfo(){return "copyright 2007-1010";}  



Life Cycle of a Servlet

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.

Stages of the Servlet Life Cycle: The Servlet life cycle mainly goes through four stages,

Loading a Servlet.

Initializing the Servlet.

Request handling.

Destroying the Servlet.

Let’s look at each of these stages in details:

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 :

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

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.

Initializing 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.

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.

The service() method while processing the request may throw the ServletException or UnavailableException or IOException.

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 :




Let’s look at each of these methods in details:

init() method: The Servlet.init() method is called by the Servlet container to indicate that this Servlet instance is instantiated successfully and is about to put into service.

//init() method

public class MyServlet implements Servlet{

   public void init(ServletConfig config) throws ServletException {

        //initialization code


    //rest of code


service() method: The service() method of the Servlet is invoked to inform the Servlet about the client requests.

This method uses ServletRequest object to collect the data requested by the client.

This method uses ServletResponse object to generate the output content.

// service() method

public class MyServlet implements Servlet{

    public void service(ServletRequest res, ServletResponse res)

    throws ServletException, IOException {

            // request handling code


    // rest of code


destroy() method: The destroy() method runs only once during the lifetime of a Servlet and signals the end of the Servlet instance.

//destroy() method

public void destroy()

As soon as the destroy() method is activated, the Servlet container releases the Servlet instance.

Related questions

+1 vote
1 answer 68 views
+1 vote
1 answer 106 views
+1 vote
1 answer 36 views
+1 vote
1 answer 99 views
+1 vote
1 answer 14 views
+1 vote
1 answer 28 views
+2 votes
1 answer 45 views
+2 votes
1 answer 5.2k views
+2 votes
3 answers 8.7k views
0 votes
0 answers 18 views
+1 vote
1 answer 22 views
Welcome to CPENTalk.com
Solution-oriented students of computer engineering on one platform to get you that