Understanding The Servlet Life Cycle

Isurie K. Liyanage
Nerd For Tech
Published in
4 min readAug 5, 2022

A servlet life cycle can be defined as the entire process from its creation to its destruction, in implementation they extended HttpServlet and either overridden the doGet() or the dopost() methods, and these actions are taken accordingly. whenever we hit the request from the client, the control automatically came inside one of the doGet() or the dopost() methods. But in java, we need an object of a class and a reference variable to invoke a method of any class. we never instantiated any of our servlet classes or call any of these methods ourselves.

From this article, Let’s see what the entire thing that goes behind the scenes where this is getting worked out automatically. We need to understand that as a part of the servlet life cycle.

Servlet Life Cycle

The following points can be pointed out in the Servlet Life Cycle. The Servlet Life cycle,

  • Entirely managed by a server container.
  • Starts when the first request comes in for that servlet.
  • Ends when the application is undeployed or the server is shut down.
  • Can be considered in two phases as below;

1. When the first request comes into the servlet,

Every servlet is identified by a URL title. After we enter the URL title and search, when the first request comes to any particular servlet, the container is going to scan that web XML file and find the appropriate servlet class as per the URL pattern. And if we have chosen the annotation way of configuration, it will scan the class accordingly. Once it finds that class, it’s going to load and instantiate that servlet class, based on the conventional class loader information that it has.

There is ClassLoader API in core Java. It uses in order to load any instantiate that servlet.

Whenever n number of requests are coming to the same servlet, there is only one instance of a servlet gets created.

Once the instance of the servlet is created, the following initialization activities happen.

➜ Servlet context and the servlet context objects are created and attached to the servlet.

➜ The incoming requests allocate a new thread.

When the application is being accessed by multiple users at the same time, these concurrent requests are handled by the multithreading capability of the Java platform.

Every request allocated new threads and those threads going to run in parallel.

➜ it also allocates a pair of HTTPServletRequest and HTTPServletResponse objects and attaches them to the incoming requests.

➜ A series of APIs are called. Here it calls init() method, service() method, and destroy() method.

service method comes from the HTTPServelet class. init() and destroy() methods come from the GenericServlet class.

The class hierarchy

init() method is used for any initialization activity of the servlet.

eg: when servlet wants to interact with DB, we can set up the DB connection inside this method.

Any kind of one-time job happens in the init() method because the init() method is executed only once during the entire life cycle of the servlet.

service() method logic depends on the kind of request made by the client such as GET, POST, etc. service() method is accordingly going to delegate the call to either the doGet() or doPost() methods. Here the request is being served to the user.

destroy() method of the servlet is called when the application is about to get undeployed, or the server is shutting down. destroy() method holds clean-up activities that want to do for the servlet.

Life Cycle methods of a servlet

So, how the life cycle looks likes for the first request is shown in Figure — How servlet life cycle looks like overall, in green color steps.

2. When repeated/multiple requests come for the same servlet,

Allocates a new thread to request, Allocates HTTPServletRequest and HTTPServletResponse are the steps that happen again for repeated requests. As it is shown in Figure — How servlet life cycle looks like overall, in orange color steps.

➜ The instance happened only once.

➜ A new thread is allocated to that request.

➜ A new pair of HTTPServlet request and response objects are created and attached with the request.

➜ Then the service() method is called directly.

Depending on the request made, the service calls either doGet or doPost, and so on and so forth.

Figure — How servlet life cycle looks like overall

APIs for Servlet Life Cycle

when summarizing methods of the servlet life cycle, there are three life cycle methods. init, service, and destroy where init and destroy can be overridden by the developer. There is no logical reason to override the service method, it has already been implemented in the HTTPServlet class.

init — can be overridden

service — must not be overridden

destroy — can be overridden

If you choose to override service, it means you take the responsibility of delegating the chores to the respective doget() or doPost() methods.

All the servlet requests that are going to come in that application are multithreaded.

We will have to synchronize code block which is trying to have multiple threads access the shared resource.

Thanks for reading, follow me to see more articles...

--

--

Isurie K. Liyanage
Nerd For Tech

Technical Writer | BSc. (Hons.) IT — UoM | Software Engineer