Servlet life cycle describes how and when a servlet object is created and initialzed, how it processes requests and how and when it is destroyed by the server. Servlet life cycle is defined by javax.servlet.Servlet interface.
Life cycle methods of Servlet interface:
init(): This method is invoked by the web server only once, just after the servlet object is created. It is used by the server to provide the reference of a ServletConfig object to the servlet and it can be used by the servlet programmer to define the initialization logic for the servlet.
ServletConfig is an interface of servlet API, implemenation of it, is provided by server vendors. An object of the class which implements this interface is provided by the server to the servlet as parameter of init() method. Role of this object will be discussed later.
service(): This method is invoked by the web server each time a request is received for the servlet. It is used by the server to get the request processed hence servlet programmers define request processing logic in it.
ServletRequest & ServletResponse are interfaces of servlet API. Their implemenation is provided by server vendors. Objects of classes which implement these interface are provided as parameter of service() method by the server. These objects facilitate transfer of request data and processing result between the server and the servlet.
destroy(): This method is invoked by the web server only once, just before the servlet is unloaded. It is used by the server to give a chance to the servlet to free the resources used by it hence it should be used by servlet programmers to define clean up operations.
In addition to these life-cycle methods, Servlet interface provides following methods as well.
getServletConfig(): This method is used by the servlet programmers to obtain the reference of the ServletConfig object which was provided to the servlet at the time of initialization by the server. When information contained in ServletConfig object will be required, its reference need to be obtained from the servlet using this method.
getServletInfo(): This method is used by the server and Tools such as IDE to display the servlet information such as author, version no and copyright etc hence it should be used by servlet programmers to provide such information. This method must not return any markup such as HTML or XML rather it should return plain text as a String.
Following diagram describes the life cycle of a servlet:
Description of the sequence diagram:
1.0 – First request is submitted for a servlet by the client.
1.1 – Servlet object is created by the server.
1.2 – ServletConfig object is created by the server for the servlet and configuration information of the servlet provided by the application developer, is stored in it.
1.3 – init() life cycle method of the servlet is invoked and reference of the ServletConfig object is provided as parameter to the servlet.
1.4 & 3.1 – A ServletRequest object is created and data sent by the client as part of the request are stored in it.
1.5 & 3.2 – A ServletResponse object is created to receive the result of processing from the servlet.
1.6 & 3.3 – A Request processing thread is started by the server.
1.7 & 3.4 – service() method of the servlet is invoked from this request processing thread and references of ServletRequest & ServletResponse objects are provided to the servlet.
1.8 & 3.5 – During processing, request data are read by the servlet from the ServletRequest object.
1.9 & 3.6 – Processing result is stored in the ServletResponse object by the servlet.
2.0 & 3.7 – After the completion of service() method, server reads the contents of ServletResponse object.
2.1 & 3.8 – These contents are sent by the server as response to the client.
3.0 – Subsequent request is submitted for the same servlet by a client.
4.0 – When the application is undeployed or the servlet is timed out, destroy() method is invoked by the server.