write a program in java to demonstrate the complete life cycle of servlet:

write a program in java to demonstrate the complete life cycle of servlet:

l have an assignment question. i don't know how to write the code for this program. my question is :Write a program in Java to demonstrate the complete life cycle of a Servlet

View Answers

June 30, 2012 at 2:26 AM

The life cycle of a servlet can be categorized into four parts:

Loading and Instantiation: The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the attribute of web.xml file. If the attribute has a positive value then the servlet is load with loading of the container otherwise it load when the first request comes for service. After loading of the servlet, the container creates the instances of the servlet.

Initialization: After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to the init() method. The init() must be called by the servlet container before the servlet can service any request. The initialization parameters persist until the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet.

The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet.

Servicing the Request: After successfully completing the initialization process, the servlet will be available for service. Servlet creates seperate threads for each request. The sevlet container calls the service() method for servicing any request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object.

Destroying the Servlet: If the servlet is no longer needed for servicing any request, the servlet container calls the destroy() method . Like the init() method this method is also called only once throughout the life cycle of the servlet. Calling the destroy() method indicates to the servlet container not to sent the any request for service and the servlet releases all the resources associated with it. Java Virtual Machine claims for the memory associated with the resources for garbage collection.

We will create a Servlet that will help you in better understanding the life cycle of a servlet.

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletLifeCycleExample extends HttpServlet {

    private int count;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        getServletContext().log("init() called");
        count=0;
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        getServletContext().log("service() called");
        count++;
        response.getWriter().write("Incrementig the count: Count = "+count);

    }

    @Override
    public void destroy() {
        getServletContext().log("destroy() called");
    }   

}

Above ServletLifeCycleExample Servlet extends HttpServlet and overrides init() Service() and destroy() methods. The servlet logs a message into server log file when servlet is initialized and sets counter to 0. Every time the service method is called, it increments the counter by 1 and displays the current value of counter to user. finally when destroy method is called, it logs a message to server log file.









Related Tutorials/Questions & Answers:

Ads