Open In App

Working of Servlet

Last Updated : 04 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Understanding how a servlet works internally helps in designing scalable, efficient web applications. The servlet follows a specific lifecycle managed by the servlet container (like Apache Tomcat), which includes loading, initializing, handling requests and finally destroying the servlet.

 To initialize the Servlet, the Web server:

  • Loads the Servlet class and creates an instance by calling the no-arguments constructor.
  • Calls the Servlet's init( ServletConfig config). It is called only once when the Servlet is loaded in memory for the first time and stored in the ServletConfig object. It’s used for setup tasks like database connections. ServletConfig provides initialization parameters (not tied to requests) and can be accessed via the getServletConfig() method, which is handled by the GenericServlet class.
  • A Java-enabled web server calls the service() method for every client request. Then developers override the service() like doGet() and doPost(). And when the servlet is no longer needed the destroy() method is called for cleanup.
Lifecycle-method-of-servlet

These lifecycle methods (init(), service(), destroy()) form the basic structure of a servlet.

Java
public dass SkeletonServet extends HttpServlet
{
    public void init()
    {
        // Initialization code goes here 
    }
    publicvoid service()
    {
        // Meaningful work happens here 
    }
    public void destroy() 
    { 
        // Free resources here  
    }
}

Every Servlet engine must follow to the Servlet lifecycle.

Initialization

To initialize a Servlet, the engine locates and loads its class, creates an instance and then calls init(ServletConfig config). This call occurs after instantiation and its timing depends on the server's configuration.

  • When the server starts (for servlets with <load-on-startup> configured).
  • When the servlet is first requested (lazy loading), just before service() is invoked.
  • Manually, at the request of the server administrator (in rare or specific configurations

init() Method

It is called once when the servlet is initialized and receives a ServletConfig object

init() is declared as follows:

public void init(ServletConfig config) throws ServletException

During initialization, the Servlet has to access two objects:

  • ServletConfig
  • ServletContext

Typical tasks performed in init() include:

  • Reading init parameters from ServletConfig
  • Loading configuration from files
  • Initializing database drivers, connection pools or logging services
  • Opening JDBC connections
  • Writing logs to a file or network resource

Service() method

After initialization:

  • The servlet handles requests via the service() method.
  • For each request, the container creates a new thread and calls service(request, response).
  • ServletRequest provides access to request data.
  • ServletResponse is used to generate and send responses.
  • Since multiple threads may call service() concurrently, it must be thread-safe (e.g., using synchronization when modifying shared data).

Destruction

The destroy() method is called once when a servlet is unloaded either by the server admin or programmatically. It won't be called again unless the servlet is reloaded and reinitialized.

destroy() Method Declaration:

public void destroy()

Typical tasks in destroy() include:

  • Releasing resources (e.g., closing database connections or connection pools)
  • Notifying external systems that the servlet is no longer active

Article Tags :
Practice Tags :

Similar Reads