Wednesday, November 2, 2016

Life Cycle Of Servlet in Java

Servlets are server side components that provides a powerful mechanism for developing server side programs. Servlets provide component-based,platform-independent methods for building web-based applications.Servlets are not designed for specific protocols. It is different thing that they are most commonly used with the HTTP protocols.In this article,we will learn about life cycle of servlet. The javax.servlet.Servlet interface defines the life cycle of methods of servlet.

Life Cycle Of Servlet:

It can be categorized into four parts:

1. Loading and Instantiation
2.Initialization
3. Servicing the Request
4. Destroying the servlet



1.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<load-on-startup> of web.xml file. If the attribute<load-on-startup> 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.

2. Initialization:

After loading 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. The init()method throws ServletExcpetion if the container can not initialize the servlet resources.

Example:

public class ServletLifeCycle extends HttpServlet
{
   static int count;
   public void init(ServletConfig config) throws ServletException
   {
      count=0;
   }
}



3. Servicing the Request:

After successfully completing the initialization process,the servlet will be available for service.  Servlet creates separate threads for each request. The servlet 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 methods of the response object.

Example:

public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException



 The doGet() method is used to handle the request and send response to the client like below

Example:

public void doGet(HttpServletRequest req,HttpServletResponse res)
thorws ServletException,IOException



public void doPost(HttpServletRequest req,HttpServletResponse res)
thorws ServletException,IOException


4.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 life cycle of the servlet. Calling the destroy()method indicates to the servlet container not to sent any request for service and servlet releases all the resources associated with it. Java Virtual Machine claims for the memory associated with the resources for garbage collection.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...