Servlet Life Cycle

Servlet is managed by servlet container, the container manages the entire lifecycle of it. From below picture, you can see how the container manage it’s lifetime and how the container handle multiple http request send to it.

servlet lifecycle senario

Steps of handle http request by the container

  1. Web server receive the client request and deliver it to the container.
  2. If the servlet is called for the first time, the container will load and initiate one servlet instance. You can do initialization actions in it’s init() method. The init() method will only run once.
  3. After initialization, the container will call it’s service(ServletRequest req, ServletResponse resp) method, and transfer client data to it by ServletRequest object.
  4. Client request will be transferred to different method of servlet by http request method. For example, if request use http get method, then doGet(HttpServletRequest req, HttpServletResponse resp) will be called, if request use http post method, then doPost(HttpServletRequest req, HttpServletResponse resp) will be called etc.
  5. If multiple request come to the same servlet instance, then the container will create multiple thread to handle the issue. Each thread will run the java code in service() and then doGet() or doPost() etc according to http request method. So servlet is designed in single instance pattern and it is not thread safe. You had better not use instance variable to save values which you do not want to share between multiple thread.
  6. When the container shutdown or restart, the destroy() method will be called before that. You can write resource release java code such as close database connection, save data to files etc in destroy(). This method is also only run once.
READ :   Hello World Example Use Servlet

Above is just a short description for servlet life cycle. we can summarize it as below.

  1. init() is called first and run only once when a servlet is loaded by container.
  2. service() is called when client request come in.
  3. doGet(), doPost(), doDelete(), doOption(), doPut() will be called according to the http request method.
  4. destroy() will be called when container shutdown or restart.
  5. After that the servlet is ready for jvm garbage collector to collect and release.

Methods list in life cycle

You can see there has a lot of methods in servlet life cycle, we will introduce them below.

  1. init()
    This is called when the container load and create the servlet instance. You can do any initialization actions such as read initialization parameters(use ServletConfig object), create db connection etc in it. The java code in init() will execute before servlet receive and process any client request. It is called only once.
  2. destroy()
    This is called before container release the servlet instance. It is only called once also. You can do any release actions in it, such as close database connection, save data to settings file, serialize java object to hard disk etc. After this, the servlet object is ready for garbage collection.
  3. getServletConfig()
    This is used to get ServletConfig object. The ServletConfig object is used to retrieve your servlet configuration parameters such as init parameters defined in web.xml.
  4. getServletInfo()
    Return basic information about the servlet, for example version, author and copy right.
  5. service()
    Container will call service() to handle request as well as write response data back to client. When the container receive a request, it will create a new thread which will run the code in service(). Then service() will call doGet() or doPost() etc according to the http request method (Get, Post, Delete, Put etc). Commonly you do not need to write code in it, you should write code directly in doGet() or doPost().
  6. doGet()
    When a client request use http get method, then doGet() will be called. A client can trigger doGet() by directly call the servlet url or set the method value to get in a http form submit.
  7. doPost()
    When a client request use http post method, then doPost() will be called. A client can trigger doPost() commonly use a form submit. It should set the form submit’s method to post first.
  8. getServletContext()
    Return ServletContext object which can be used to interact with the container. You can use this object to get web application’s context path, get init parameter value etc. You can click here to see detail introduction of ServletContext.
  9. getInitParameter(String name)
    This is used to get init parameter values defined in web.xml file.
READ :   Java Servlet Interview Questions and Answers

 

(Visited 155 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.