Java Servlet life cycle consists of a series of events that begins when the Servlet container loads Servlet, and ends when the container is closed down Servlet. A servlet container is the part of a web server or an application server that controls a Servlet by managing its life cycle. Basically there are three phases of the life cycle.
First, the Servlet container loading Servlet in the memory, creating a servlet object and initializes it. Second, Servlet object services requests mapped it by the Servlet container. Thirdly Servlet container shuts down Servlet and Java Virtual Machine (JVM), which is the environment that runs Java applications, freeing the computer resources that the Servlet.
Three of the five methods defined by the Servlet interface — init (), service () and destroy () —‘s life cycle methods.
We’ll be covering the following topics in this tutorial:
The init() method:
The Servlet container running a Servlet’s init () method, which initializes Servlet, once in the Servlet life cycle, after loading the Servlet and create a Servlet object. The init method definition looks like this:
public void init(ServletConfig config) throws ServletException {
// Initialization code...
}
When instantiating the servlet container passes an argument to the init() method a ServletConfig object for loading specific configuration settings to the servlet.
If an error occurs upon calling the init () method, it throws an exception of type and ServletException servlet is not initialized.
The service () method
The container runs the Servlet service () method for each request from a client, a web browser. The service method definition looks like this:
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
}
The ServletRequest and ServletResponse objects are automatically passed as a parameter to the service () method by the container.
The destroy () method
The Servlet container running a servlet’s destroy () method once in a lifetime to close the Servlet. The destroy method definition looks like this:
public void destroy() {
// Finalization code...
}
The destroy ( ) method waits until all threads started by the service () method before terminating the execution of the servlet.
Note– The init() and destroy() method only once called during its servlet life cycle.
Architecture Diagram of Servlet Life Cycle:
The servlet is loaded at server startup or when the first request. The servlet is instantiated by the server. The init ( ) method is invoked by the container. In the first application, the container creates specific Request and Response objects to the query.
The service () method is called for each request in a new thread. The Request and Response objects are passed as parameters. Through the Request object, the service () method will be able to analyze information from the client With the Response object, the service () method will provide a response to the client.
This method can be executed by multiple threads; there should be exclusion for the use of certain resources processes.
The destroy () method is called when unloading the servlet, that is to say when it is no longer required by the server. The servlet is then reported to the garbage collector.