What is Java Servlet Life Cycle and how to explain it

servlet is managed through a well defined life cycle that defines how it is loaded and instantiated, is initialized, handles requests from clients, and is taken out of service. This life cycle is expressed in the API by the init, service, and destroy methods of the javax.servlet.Servlet interface that all servlets must implement directly or indirectly through the GenericServlet or HttpServlet abstract classes.

Stages of Servlet Lifecycle:

A servlet passes through the following stages in its life. 
  • Initialize
  • Service
  • Destroy




What is Java Servlet Life Cycle,Stages of java Servlet Lifecycle, what are different java servlet lifecycle stages, what is java servlet,  what is java, java tutorial, java servlet tutorial, java ee tutorials, what is java servlet based web server, java web action, java web applications development, javawebaction

Initialize

When the servlet is first created, it is in the initialization stage. The web server invokes the init() method of the servlet in this stage. It should be noted here that init() is only called once and is not called for each request. Since there is no constructor available in Servlet so this urges its use for one time initialization just as the init() method of applet

Initialize stage has the following characteristics and usage
  • Executed once, when the servlet gets loaded for the first time
  • Not called for each client request
  • The above two points make it an ideal place to perform the startup tasks which are done in constructor in a normal class.

Service

The service() method is the engine of the servlet, which actually processes the client’s request. On every request from the client, the server spawns a new thread and calls the service() method as shown in the figure below. This makes it more efficient as compared to the technologies that se single thread to respond to requests.

what is java servlet based web server, What is Java Servlet Life Cycle,Stages of java Servlet Lifecycle, what are different java servlet lifecycle stages, what is java servlet,  what is java, java tutorial, java servlet tutorial, java ee tutorials, explain java servlet life cycle,  java web development, java web action, javawebaction, java web hosting

The figure below show both version of the implementation of service cycle. In the upper part of diagram we assume that servlet is made by sub-classing from GenericServlet (Remember, GenericServlet is used for constructing protocol independent servlets.). To provide the desired functionality, service() method is overridden. The client sends a request to the web server, a new thread is created to serve this request followed by calling the service() method. Finally a response is prepared and sent back to the user according to the request.

What is Java Servlet Life Cycle, java servlet httpServlet and GenericServlet subclass, Stages of java Servlet Lifecycle, what are different java servlet lifecycle stages, what is java servlet,  what is java, java tutorial, java servlet tutorial, java ee tutorials, explain java servlet life cycle, servlet request responce, java web development, java web action, javawebaction, java web hosting


The second part of the figure illustrates a situation in which servlet is made using HttpServlet class. Now, this servlet can only serves the HTTP type requests. In these servlets doGet() and doPost() are overridden to provide desired behaviors. When a request is sent to the web server, the web server after creating a thread, passes on this request to service() method. The Service() method checks the HTTP requests type (GET, POST) and calls the doGet() or doPost method depending on how the request is originally sent. After forming the response by doGet() or doPost() method, the response is sent back to service() method that is finally sent to the user by the web server.

Destroy

The web server may decide to remove a previously loaded servlet instance, perhaps because it is explicitly asked to do so by the server administrator, or perhaps serlet container shuts down or the servlet is idle for a long time, or may be server is overloaded. Before it does, however it calls the servlets destroy() method. This makes it a perfect spot for releasing the acquired resources.


The following figure can help to summarize the life cycle of the Servlet


What is Java Servlet Life Cycle,Stages of java Servlet Lifecycle, what are different java servlet lifecycle stages, what is java servlet,  what is java, java tutorial, java servlet tutorial, java ee tutorials, explain java servlet life cycle, servlet request responce, java web development, java web hosting, java web action, javawebaction

The web sever creates a servlet instance. After successful creation, the servlet enters into initialization phase. Here, init() method is invoked for once. In case web server fails in previous two stages, the servlet instance is unloaded from the server. After initialization stage, the Servlet becomes available to serve the clients requests and to generate response accordingly. Finally, the servlet is destroyed and unloaded from web server.
What is Java Servlet Life Cycle and how to explain it What is Java Servlet Life Cycle and how to explain it Reviewed by Anonymous J on 22:30:00 Rating: 5
Powered by Blogger.