What is a Servlet?

Servlet Flow


Java uses so-called Servlets as the component that handles a request from a user (or other systems) and generates a response based on the request. Although with frameworks like Spring MVC you might not deal with Servlets directly. However, it is useful to understand the underlying principles of a Servlet, as you will come in touch with the terminology many times in your code.
Java Servlets are Java Classes that conform to the Java Servlet API and run inside of a so-called Servlet Container. A Servlet Container is part of an Application Server (such as Apache Tomcat). The Servlet Container manages
  • the lifecycle of a Servlet,
  • access control to the Servlet, and
  • mapping URLs to the appropriate service.
Servlets can understand any client-server protocol, but most often the HTTP protocol is used. Servlets are part of a web application – packaged in a so-called war-file and deployed on the server.
Servlet Container and Servlet Lifecycle
Servlet Container and Servlet Lifecycle

Instantiation and Initialisation

Basically, a Java Servlet is an instance of javax.servlet.http.HttpServlet. To be correct here, as HttpServlet is an abstract class, a specific Servlet is an instance of a class that extends HttpServlet and implements one of its methods. In an application, you might have several servlets, each one responsible for a different group of requests (e.g. a servlet to handle a shopping basket, one to handle the payment-process).

A Servlet is generally instantiated when the Server starts-up. A Servlet is initialised (the init()-method is called) the first time it is requested (although it can be configured that it is initialised at startup in the configuration of the Servlet-Container).

In general, only one instance of a specific Servlet is instantiated by the Servlet Container. This Servlet object is then shared by all requests. For each request, the Servlet Container creates a thread (or takes a thread from a pool of threads) to handle the request. In addition, for each request, a new pair of HttpRequest and HttpResponse instances is created. This thread then calls the service()-method of the Servlet instance. So multiple threads can access the service()-method (which calls one of the do...()-methods) of the Servlet instance simultaneously.

Servlet Handling Multiple Requests
Servlet Handling Multiple Requests (one instance of each Servlet, multiple threads to handle the requests)

The configuration of the Servlet is described in the web.xml (which is part of the web application). The configuration and state of the Servlet-Container is held in thejavax.servlet.http.ServletContext, e.g. you can set attributes are stored during the lifetime of the Container.

package com.vividbreeze.servletdemo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public void init() throws ServletException {

  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

Request Handling

When a request is made to the Server, an instance of java.servlet.http.HttpServletRequest is created and filled with the HTTP-Request data, similar for a response a HttpServletResponse instance is created.

In the image above, I also depicted Listeners and Filters. Filters can be used to monitor or modify requests and responses, a Listener listen to events in a Web Container, e.g. Session-creation and more.

The Servlet that handles the request is defined in the web.xml


You call the servlet by a URL of the form http://[hostname]:[port]/[servlet-name]. If we assume the application above runs on your localhost on port 8080, the URL would be http://localhost:8080/HelloServlet. Parameters can be transferred via get-request (Parameters are in the URL) or post-request (Parameters are inside the message body).

The WebContainer passes the HttpServletRequest-Object to the service()-method, which then, depending on the request-method calls the corresponding doGet(), doPost(), … methods. These methods interpret the request data, calls some business-logic and fill the HttpServletResponse-Object, which is passed the same way back to the client.

Server Shutdown

The destroy()-method of the Servlet is called when the server shuts down. This method is usually used to release resources used by the Servlet.


Of course, there is more to Servlets than described here briefly. When you work with technologies that build on Servlets, such as Java Server Pages (JSP), Java Server Faces (JSF), Apache Struts, Spring MVC and others, you might not come directly in touch with the underlying concepts. However, often things don’t run smoothly it helps to understand the basic technologies.

Further Readings

Java Servlet Programming (J. Hunter & W. Crawford, 1998) – Chapter 3. The Servlet Life Cycle (this reference seems old (and some parts are outdated), but I have found no other documentation online that so thoroughly explains the lifecycle of a Servlet)