Servlet is managed by the servlet container, the container manages the entire lifecycle of the servlet. When the servlet container startup, it will initialize several servlet instances, at this time the servlet
init() method will be invoked. When the servlet container receives an HTTP request, it will execute the servlet’s
service() method, and when the servlet container shutdown, it will call the servlet
destroy() method. Multiple threads can run one servlet instance, so the servlet is not thread-safe.
1. Steps Of Handle HTTP Request by The Servlet Container.
- The web server receives the client request and delivers 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 its
init()method will only run once.
- After initialization, the container will call its
service(ServletRequest req, ServletResponse resp)method, and transfer client data to it by ServletRequest object.
- Client requests will be transferred to the different methods of servlet by the HTTP request method. For example, if the request uses the HTTP get method, then
doGet(HttpServletRequest req, HttpServletResponse resp)will be called, if the request uses the HTTP post method, then
doPost(HttpServletRequest req, HttpServletResponse resp)will be called etc.
- If multiple requests come to the same servlet instance, then the container will create multiple threads to handle the issue. Each thread will run the java code in
doPost()etc according to the HTTP request method. So servlet is designed in a single instance pattern and it is not thread-safe. You had better not use an instance variable to save values that you do not want to share between multiple threads.
- 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 the
destroy()method. This method is also only run once.
Above is just a short description of the servlet life cycle. we can summarize it as below.
- init() is called first and runs only once when a servlet is loaded by the servlet 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 the JVM garbage collector to collect and release.
2. Methods List In The Servlet Life Cycle.
You can see there has a lot of methods in the servlet life cycle, we will introduce them below.
- 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 the servlet receives and process any client request. It is called only once.
- destroy(): This is called before the servlet 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 the settings file, serialize java object to hard disk, etc. After this, the servlet object is ready for garbage collection.
- 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.
- getServletInfo(): Return basic information about the servlet, for example, the version, author, copyright info.
- service(): The servlet container will call service() to handle requests as well as write response data back to the client. When the container receives a request, it will create a new thread that will run the code in service(). Then service() will call doGet() or doPost() 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().
- doGet(): When a client request uses 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 an HTTP form submit.
- doPost(): When a client request uses HTTP post method, then doPost() will be called. A client can trigger doPost() commonly use a form submit. It should set the form submit method to post first.
- getServletContext(): This method will return the ServletContext object which can be used to interact with the servlet container. You can use this object to get the web application’s context path, get init parameter value, etc. You can click here to see a detailed introduction to ServletContext.
- getInitParameter(String name): This is used to get init parameter values defined in the web application web.xml file.