Java Servlet lifecycle

The Java Servlet life cycle consists of a series of events that begin when the Servlet container loads the Servlet and ends when the container closes Servlet. A servlet container is part of a Web server or application server that controls a servlet that manages its life cycle.

First, the container servlet loading Servlet into memory, creating a servlet object and initializing it. Second, Servlet object service requests were assigned by the Servlet container. Third, the Servlet container closes the Servlet and the Java Virtual Machine (JVM), which is the environment that runs Java applications, freeing the resources from the team that Servlet.

Three of the five methods defined by the Servlet interface life-cycle methods

init (), service () and destroy () .

The init () method

The Servlet container executing the init () method of the servlet, which initializes the servlet, once in the servlet life cycle, after loading the servlet and creating a servlet object. The definition of the init method is 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 to load specific configurations in the servlet.

If an error occurs when calling the init () method, it will throw a type exception and the servletException will not be initialized.

The service () method

The container executes the servlet service () method for each request from a client, a web browser. The definition of the service method is as follows:

public void service(ServletRequest request,
 ServletResponse response)
 throws ServletException, IOException{
 }

The ServletRequest and ServletResponse objects are automatically transmitted as a parameter to the service () method by the container.

The destroy () method

The Servlet container executing the destroy () method of a servlet once in a lifetime to close the servlet. The definition of the destroy method is like this:

public void destroy () {}

// Finalization code... 

The destroy () method waits until all chaining is initiated by the service () method before the servlet is finished executing.

Note – The init () and destroy () method are only called once during the servlet life cycle.

Interested in learning Java? Enroll now:” Java training in Chennai “

Servlet Lifecycle Architecture:

servlet life cycle architecture

The servlet is loaded at the start of the server 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 for the query.

The service () method is called for each request in a new chain. The Request and Response objects are passed as parameters. Through the Request object, the service () method can analyze the client information. With the Response object, the service () method will provide a response to the client.

This method can be executed by several subprocesses; there must be exclusion for the use of certain resource processes.

The destroy () method is called when you download the servlet, that is, when it is no longer required by the server. The servlet is then reported to the garbage collector.

 

To getting expert-level training for Java Training in your location – java training in Chennai | java training in Bangalore  | java training in Pune | java training in Chennai | java training in Bangalore | java training in Chennai | java interview questions and answers |  core java interview questions and answers | java training in Chennai | For getting java online training | java online training

Leave a Comment