×
DocsConsole

DocsConsole

Servlet Life Cycle

By | | Updated : 2019-12-13 | Viewed : 208 times

Servlet Life Cycle

Servlet Life Cycle is nothing the process of Servlet execution. Servlet and its life cycle have been managed by Servlet Container. Servlet Life Cycle contains four main phases which are combinedly defined as Servlet Life Cycle.

    Servlet Life Cycle Phases

  • Servlet Loading

  • Servlet Instantiation & Initialization

  • Servlet Request Processing

  • Servlet Destroy

Let’s look into all these phases in detail. The below given diagram will depict the entire life cycle.

Servlet Life Cycle Flow

Servlet Loading

Servlet Loading is the first phase of Life Cycle. Typically Servlet loading will be performed only once. When client sends first request to the Web Server for fetching the response, then Servlet container will load the Servlet. If load-on-startup tag configured in web.xml, then Servlet container will load the Servlet at the time of deploying.

Servlet Instantiation & Initialization

In Servlet instantiation phase, Servlet Object (HelloWorldServlet) will be created by the container. It will be done after loading of the Servlet. In Initialization phase, Servlet container will create the object for ServletConfig to use init(ServletConfig) of the GenericServlet Class. While executing the business logic, Business need some config parameter such as DB details. Here ServletConfig can provide all this kind of information. init(ServletConfig) is not convenient method. In the process of execution of init(Servletconfig), Servlet Container will invoke init() which is convenient method for developer. Conviment Method means, developer can overwrite and can code his own logic. If init() is overridden in the Servlet (HelloWorldServlet) class then overridden method will be executed. Please observe the below given code snippet.

GenericServlet.java
public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable {

	private transient ServletConfig config;

	public void destroy() {
	}

	public ServletConfig getServletConfig() {
		// Here ServletConfig object will be created
	}

	public ServletContext getServletContext() {
		// Here ServletContext object will be returned
	}

	public void init(ServletConfig config) throws ServletException {
		this.config = config;
		this.init();
	}

	public void init() throws ServletException {
	}

	public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;

}
HelloWorldServlet.java
public class HelloWorldServlet extends GenericServlet {

	public void init() throws ServletException {
		System.out.println("Servlet Initiated");
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println("Servlet Request will be processed here.");
	}

	public void destroy() {
		System.out.println("Servlet will be destroyed.");
	}
}

If you observe the GenericServlet, it contains the init (ServletConfig config). It will be called by the Servlet Container. Here in this method there is one more method init().

In GenericServlet, the init() method is there with empty body. But we have overridden init() in the HelloWorldServlet class . So, overriden iniit() will be executed.

Servlet Request Processing:

Here service(ServletRequest req, ServletResponse res) method will be executed in this phase. Here Code will be written for the preparing the response. Foreach request, one new thread will be created. Then container calls service(req,res) method for preparing the response. Means Service(req, res) method will be executed once per request. Container will prepare Request, Response for passing to the Service(req, res).

Servlet Destroy

If you observe destroy() method in the GenericServlet. This is one of the convenient method for overriding. If we override this method as like in HelloWorldServlet, then destroy() will be executed.

Leave A Reply