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.
Steps of handle http request by the container
- Web server receive the client request and deliver it to the container.
- 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.
- After initialization, the container will call it’s service(ServletRequest req, ServletResponse resp) method, and transfer client data to it by ServletRequest object.
- 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.
- 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.
- 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.
Above is just a short description for servlet life cycle. we can summarize it as below.
- init() is called first and run only once when a servlet is loaded by container.
- service() is called when client request come in.
- doGet(), doPost(), doDelete(), doOption(), doPut() will be called according to the http request method.
- destroy() will be called when container shutdown or restart.
- 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.
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.
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.
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.
Return basic information about the servlet, for example version, author and copy right.
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().
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.
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.
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.
- getInitParameter(String name)
This is used to get init parameter values defined in web.xml file.