A Servlet Container is a key component of Java web applications that provides a runtime environment for executing servlets. It acts as an intermediary between the web server and servlets, handling client requests and generating responses. It simplifies development by managing lifecycle, security, and request processing.
- Controls servlet execution using init(), service(), and destroy() methods
- Receives HTTP requests and routes them to the appropriate servlet.
- Manages user sessions and provides authentication and authorization features.
Architecture of Servlet Container
The diagram shows how different components interact in a Java web application using a servlet container.

Concepts of Servlet Container:
1. Client
- The client (browser) sends an HTTP request.
- The web server (labeled as Web Listener) receives this request.
2. Web Listener
- The web server forwards dynamic requests to the servlet container.
- The servlet container manages the processing and sends results back to the web server.
3. Servlet Container
- The container identifies the correct servlet based on URL mapping.
- It sends the request to the servlet for processing.
4. Servlet Processing
- The servlet handles business logic.
- If needed, it connects to the database using JDBC.
5. Database (Data Source)
- The servlet interacts with the data source to fetch/store data.
- Results are returned to the servlet.
6. Response Flow Back
- Servlet ->Servlet Container -> Web Server -> Client
- The processed response (HTML/JSON) is sent back to the user.
Example:
package com.example;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
// Define the Servlet URL pattern using @WebServlet annotation
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
// Override the doGet method to handle GET requests
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set the content type of the response to text/html
response.setContentType("text/html");
// Get the PrintWriter object to write the response output
PrintWriter out = response.getWriter();
// Write a simple HTML page as the response
out.println("<html>");
out.println("<head><title>Hello Servlet</title></head>");
out.println("<body>");
out.println("<h1 align = center style = 'color:blue;' >Hello, World! This is a simple servlet.</h1>");
out.println("</body>");
out.println("</html>");
// Close the PrintWriter
out.close();
}
}
Output:

Explanation:
- Servlet Annotation (@WebServlet ("/hello")): This map the servlet to URL pattern /hello.
- deGet Method: It is handle the HTTP GET requests.
- response.setContentType ("text/html"): It is tell a browser that response is HTML.
- PrintWriter out = response.getWriter(): It is get the writer to output the response.
- out.println(...): It is used to send the HTML response to browser.
Popular Servlet Containers
Several servlet containers are widely used in Java web development to run servlets and manage the request-response cycle.
- Apache Tomcat : A lightweight, open-source, and most widely used servlet container developed by the Apache Software Foundation.
- Jetty :A fast and lightweight container, ideal for microservices and embedded applications.
- GlassFish : A full-featured application server that includes a servlet container along with enterprise tools.
- IBM WebSphere : A robust enterprise-level server with advanced features and high performance.
- Oracle WebLogic : A powerful enterprise server widely used for large-scale applications.