Understanding Servlet Lifecycle

A Quick Recap:

Before we start this tutorial lets start a quick recap of what we have learned in our previous tutorials. We have seen three different objects request, session and context objects. Here request object is created on every Url access, or when you click on a link, these objects can also be created when we submit a form. A session object is something that is available throughout the user sessions for a specific servlet whereas the context object is something that is available across all servlets for the whole application and across all users. To make use of the session and the context object we pull them from the request object as mentioned in the below source. In this tutorial, you will learn servlet lifecycle along with execution flow of servlets.

Source:

There is another thing that we have learned, and that is the creation of the servlet object. The servlet object is not created on each URL access. Each URL access (request) is treated as a separate thread. Due to threading, it becomes easier for our application to scale to the number of users easily.

Servlet Lifecycle

Remembering all the things that we have learned. To understand the servlet we have to understand Life cycle of the servlet. When we try to understand servlet, there is a question arises that What is the servlet flow of execution?

Overview:

Till know we know that one of the responsibilities of the container is the creation of the servlet object, now we will see in detail about the servlet creation, what other objects are needed by the container and what other methods are used by the container to handle a dynamic request. There are some objects and methods that are used by the container during the servlet life cycle. these objects and methods are init(context), service(request,response), destroy(), config object, request/response object, servlet object and the context object. We well discuss all of them within the Servlet Lifecycle. Let’s understand it through big picture of the scenario. The given figure bellow is the topology for the Client-Server Architecture in which our web application (SimpleServletProject) is running.

Servlet Flow of Execution
Servlet Flow of Execution

Explanation

1 – When the server is started, the Container will deploy all the applications and create a separate “Servlet Context object” for each application. In our figure, we have only one web application named Simple Servlet Project, so one context object is created.

2- In the browser when the user makes a dynamic request through a URL.

3- A request is made and passed through the (HTTP)protocol, this request has all the data about the particular URL request.

4- This request then forwarded to the main server, here server will perform request validation process.

5- Only the valid requests are then passed to the container.

6- Container will then read the request and identifies the requested resources(HTML Page OR Servlet),

7- Container then reads the web.xml to identify the particular servlet that is requested. Identification is done through URL mapping.

8- Container after searching looking the URL mappings inside the web.xml identifies that the requested resource is MyFirstServlet, its bye code is then generated in MyFirstServlet.class

9- Now the servlet “Lifecycle” starts in the operational area of the server, Container first loads the servlet into the memory.

10- Container then creates the “Servlet object” inside the memory and made doGet() and doPost() methods available to use, this creation process is called Servlet Instantiation.

11- After the instantiation the Servlet Initialization is performed, for which container have to call init((ServletConfig config) method. This init() method will be called only once after the creation of the servlet.

12- The init(-) method requires a config object so a new object will be created of type ServletConfig. ServletConfig object is then passed in the init(-) method and the init method will be executed. After the execution, a new thread will be created against the user request. Each user request is passed to a new thread

13- To handle the Http request Container have the service(ServletRequest request, ServletResponse response)  method. Service method is called each time when the request for the servlet arrives on the server.

14- To handle the destruction of objects the container have the method. destroy()  method is called when the servlet is no longer needed.

Servlet LifeCycle State Diagram:

State chart of Servlet Lifecycle in Java EE
State chart of Servlet Lifecycle in Java EE

 

Download Complete Servlets Project Here

Summary:

In this tutorial, we have accomplished following things.

  • Learned about the Servlet Lifecycle
  • Learned init(),service() and destroy() Methods

In the next tutorial, we will learn Servlet Filters so stay tuned and stay blessed.