0

How can I use the Servlet's Life Cycle?

Posted by Guillermo GarcĂ­a on 1:27 PM in , ,
Knowing the servlet's life cycle is very important to get all the benefits of been a servlet. If you handle it well, you will improve your web application in terms like performance and security without much effort.

The life cycle steps (for HttpServlets):

  1. Class loading: The container will load the class of the servlet (based in the tag in the web.xml).
  2. Create the object: The container will instatiate the servlet (creating an object, not yet a servlet)
  3. Create the servlet: The container will call the init(ServletConfig config) method. Here is when the container promote an object to a Servlet, giving him a ServletConfig object and access to ServletContext (ApplicationContext must be named).
  4. Handling a request: When a request arrive to the container, It will create a thread, and this new thread will call the service(HttpServletRequest, HttpServletResponse) method. So, each request have is own thread and memory stack.
  5. Calling the correct HTTP method handler: The service() method will call the Http method handler. For example, if the HTTP request uses the GET Method, the service() method will call the doGet(HttpServletRequest, HttpServletResponse) method.
  6. Destroy the servlet: The container, based in his own reason will call the destroy() method. This make the servlets instance elegible for the garbage collector.
So, based in this life cycle, we can say that the servlet has only two state: Doesn't exist (before step 3 and after step 6), and Exist (after step 3 and before step 6) .

But now, lets talk about a few things that may appear in each developers head:
  • Why I must use the init() method instaed of the class constructor? You can use it. In fact, you can use both. But you must be aware of this: In the constructor, the servlet is not yet a servlet. At this point (step 2) the servlet doesn't have access to the ServletConetxt so you can't get init parameters defined in the web.xml, for example. So, in the constructor, the servlet is a simple and vulgar java object. Because of this, most of the developers do all in the init() mehtod, because there they are allow to do all that a java class an a Servelts needs.
  • Each request instatiate a servlet? so ¿there is more than one servlet instance? Nop, if you look at the life cycle, you can find that the first three steps (creating of the servlet) happens only one time in the life cycle. The step 4 is where the request take action, and as you can see, the servlet is already instantiated and created. So, there is only one Servlet instance (per servlet definition in the web.xml)
  • Can I use the init() method for prepare the servlet for handling the newly arrived request? Nop. The init() methods runs only once at the begining, not per each request. The init() method is mostly used for getting resources and objects that the servlet will share with all the request handled by him.
  • So, If all the resources and objects obtained in the init() method are share with all the requests, I must handle concurrent access to those resources? Yes, if you get a resource in the init() method, the only way that the doXXX() methods can used is by an instance variable, so, if there is only one reference to the resource and many, many threads trying to use it, what you get? A concurrent scenario. You can handle it using the wells knowns "synchronized" technics, but most of the time those resources are "read or consult - only", so, concurrent issues may not appear.
  • Can I overwrite the service() method? Yes, but you shouldn't do it. For Http Servlets the service method implements all the logic to identify the HTTP method and call the correct servlet method handler (like doPost()). If you want to handle requests (and you must handle them) you must implement the correct doXXX() method.
  • For what can I use the destroy() method? For formally and correctly realease of resourses that the servlet holds. For example, if the servlet have a non conventional access to a non conventional legacy system, and this access must be close in a very complex way (if you don't want to crash the legacy system), the destroy() method allows you to close this communication correctly before the container destroy the servlet (in a server restart or an application re-deploy, for example).

0 Comments

Copyright © 2009 ggarciao.com
- Cup of Java -
All rights reserved.