Monday, December 6, 2010

Servlet Life Cycle

As we know the conventional J2EE application consists of set of Servlet and JSP files. therefore it is good to get a proper understanding of the servlet and jsp including their role and lifecycle. today i am going to discuss the servlet life cycle and hope to discuss the JSP in the future post. therefore we will briefly look at the Lifecycle and the role of the servlet in this post.

The Responsibility of the Servlet

the major responsibility of the servlet is to take the client request and send back the response.that means the servlet will be responsible for implementing and the processing business logics. In traditional MVC based J2EE web applications, the servlet act as the Controller.

M (Model) :- contains of POJO classes/core java classes to represent real word entities.

V (View) :- contains a set of JSP pages to implement presentation logic for the user.

C (Controller) :- contains the servlets to implement and process business logics.


The lifecycle of the Servlet

the servlet lives in the servlet container. that means the life cycle of the servlet is handled by the servlet container.servlet container provide necessary services for the servlet from born to dead.Apache tomcat is one of the well known servlet/web container.

i will explain the life cycle of the servlet using the following example.

suppose user clicks the following URL in the web application.

http://localhost:8080/example/sampleServlet

1.) this request will initially come to the web container. then the web container will identify that the request is to invoke a servlet. Then the web container will create two seperate objects known as HttpServletRequest and HttpServletResponse Objects for that request (in this case the web container doesn't know the what the actual servlet is).

2.) after creating the HttpServletRequest and HtppServletResponse objects, the web container will figure out the actual servlet based on the request URL.then it will check whether the Servlet is initialized. if it is not initialized, the container will initialize the servlet first.(the servlet initialization process will bemethod parameters) discussed later)Assume that the servlet is initialized at the moment.the web container will invoke the service() method of the servlet by providing(as the HttpServletRequest and HttpServletResponse Objects created. finally the web container will create and allocate a seperate thread to serve for that particular request.


3.) the service() method will figure out which servlet method to invoke based on the HTTP request methods.(GET or POST). if it is HTTP GET request, then it is required to invoke doGet() method. if it is HTTP POST, then doPost() method should be implemented.The service method will invoke either doGet() or doPost() methods providing the HttpServletRequest and httpServletResponse objects gained.



The servlet initialization process

the servlet must be initialized before handing client requests (invoking service() method). the servlet initialization is done by the servlet container by invoking inti() method. the init method is called(or servlet initilaization is done) only once for the life time of the servlet.servlet intilaization process can be described as follows.

1.) web container loads and instantite( create the object of the servlet) the servlet class.

2.) the created object is still not a servlet object. it is just an object cretaed from the servlet class type.In order to become a servlet, it is required to grant the servlet priviledges for that object. it is done by invoking init() method on that object. init() method is called only once throughout the life time of the servlet.

3.) after completing the init() method, servlet is initialized. therefore it is possible to invoke service() method to handle client requests.it is recommneded the developers to not to override the init() method.but if you required you can override the init() method. in such case, make sure that  you have all the initialization logic(codes) for that servlet(eg:- database connection codes, other connection and resource handling codes)


destroy()

The servlet container invoke the destroy() method on the servlet object to make it eligible for garbage collection.after completing all the works with the servlet, we do not require the servlet object longer. at that time, the servlet container invoke the destroy() method on the servlet object for releasing all the resorces(de-allocating) allocated during the initialization process. the destroy() method is also called only once like the init() method.

hope this will helpful for you!



regards
Chathuranga Tennakoon
chathuranga.t@gmail.com











No comments:

Post a Comment