:::: MENU ::::

Java Servlet Introduction, Overview and Life Cycle

Java Servlet Introduction, Overview and Life Cycle

Servlet is a one the most famous web technology till date. It was the first successful and heavily consumed web technology for Java. Since then new technologies have come but the glory remains the same for the Java Servlets. Servlets are very useful for understanding the latest trend of programming and for certain use cases.

Java Servlet is a part of the Java Enterprise Edition (Java EE). In order to work with it, we need a compatible “Servlet Container” (e.g. web server) where we put our Servlets.

Today we will not go for a very detailed description of Java Servlets. Rather, we will quickly get up to speed and smartly grasp the most important concepts of the technology.

Overview

A Java Servlet is a Java object that responds to HTTP requests. It runs inside a Servlet container. For the matter of fact the web server doesn’t pass the response directly to the servlets, Servlet container does that. The Servlet container wraps all the servlets inside and handles all the features listed below.

1. Communications Support– Provides easy way for servlets to communicate with web server.
2. Life Cycle Management– It controls the life and death of a servlet.
3. Multithreading Support– It automatically creates a separate Java thread for every servlet request it gets.
4. Declarative Security– Container provides us the deployment descriptor(XML configuration) to configure our servlet application, It is secured because its loosely coupled with the core servlet class.
5. JSP Support– It translates the JSPs to core  Java.

Below illustration will show us how a HTTP request is handled by the servlet container. We are not specifying Servlet container separately, we’ll assume it as a logical wrapper inside the web server:

Java Servlet Introduction, Overview and Life Cycle

A Servlet is part of a Java web application and its nothing but a Java class object. Sometimes a Servlet container (web server) may run multiple web applications at the same time, each having multiple servlets running inside. Here is an illustration of that:

Java Servlet Introduction, Overview and Life Cycle

A Java web application can contain other components than servlets. It can also contain Java Server Pages (JSP), Java Server Faces (JSF) and Web Services.

HTTP Request and Response

When the browser sends an HTTP request to the Java web server, it checks if the request is for a servlet. If it is, the servlet container gets the request. The servlet container will then identify which servlet the request is for, and activate that particular servlet.

Once the servlet has been activated, the Servlet container creates the request and response object. There after the servlet processes the request object, and sends back the response. The response is then sent back to the browser.

Servlet Containers

Servlet containers basically runs inside a Java web server. We can check out few common well known, free Java web servers like:

Servlet Life Cycle

A servlet has a certain life cycle that is managed by the Servlet container. The life cycle contains the following steps:

1. Loading of Servlet Class.
2. Creating the Servlet Instance.
3. Calling the init() method.
4. Calling the service() method.
5. Calling the destroy() method.

First three steps are executed only once, when the servlet is initially loaded. By default the servlet is not loaded until the first request is received for it. We can force the container to load the servlet when it starts up. We will see that when we will configure our web.xml for Servlet Configuration.
service() method executed multiple times – once for every HTTP request to the servlet.
destroy() method is executed when the container unloads the servlet.
Now the detailed description for each step:

Java Servlet Introduction, Overview and Life Cycle

Loading The Servlet Class

Before a servlet can be invoked the container must first load the particular class definition. This happens just like any other class definition gets loaded.

Creating The Servlet Instance

When the servlet class is loaded, the servlet container creates an instance of the servlet.
Notably, only a single instance of the servlet is created, after that the same identical requests are executed on the same servlet instance with a new Thread. Again this is up to the servlet container to determine, but generally, there is just one instance.

Calling The init() Method

When a servlet instance is created, the container invokes its init() method. The init() method gives us the privilege to configure a particular servlet before the very first request is processed.
We can specify init parameters to the servlet in the web.xml file or via annotations . We will see that very soon for more details.

Calling The service() Method

For every request received to the servlet, the container calls servlet’s service() method. Our servlet extends HttpServlet class(or GenericServlet), to process the request we must have to override one of the doGet() / doPost() or service() method because these methods are typically called by the container internally.
As long as the servlet is active in the container, the service() method can be called.

Calling The destroy() Method

When a servlet is unloaded by the servlet container, the destroy() method gets a call. This happens only once, because a servlet is only unloaded once in its life cycle.
Any particular servlet is unloaded by the container(i.e web server) if the container shuts down, or if the container reloads the whole web application at runtime.

Conclusion

This concludes the basic overview and the life cycle of java servlet. In our next tutorial we will setup our editor for our development and practice some examples for better understanding. We will be describing each of its major components along the way.

Thank you for your time.

Indrajit Das
Follow me

Indrajit Das

Co-founder of CodeStrive [ Kind of a seeker for new dimensions in WEB world, sort of a Thinker, Blogger, Die-Hard Coder, Wash-Room Singer, Football Player, Gamer, Guitar Player, Cartoonist and the list goes on.. ]
Indrajit Das
Follow me

Latest posts by Indrajit Das (see all)

Hands On Java Servlet With Eclipse and Apache Tomcat
Pride that quarells at self-breath