Servlet Architecture defines how Java web applications handle client requests and generate dynamic responses using server-side components called servlets. It works on a request-response model where the web server interacts with servlets through a servlet container. This architecture ensures efficient processing, scalability, and platform independence for web applications.
- Manages the lifecycle of servlets, including loading, initialization, execution, and destruction.
- Handles client requests (HttpServletRequest) and sends responses (HttpServletResponse).
- Important methods include init(), service(), and destroy() which control servlet execution.
Real world example: When a user clicks the “Buy Now” button on an online shopping website, the browser sends a request to the server, which is handled by a servlet. The servlet processes the request by checking product details and placing the order, then sends a confirmation response back to the user.
Key Components of Servlet Architecture
Servlet Architecture consists of the following main components:

Servlet Architecture Flow (Step-by-Step):
1. Client (Web Browser)
- The client is usually a web browser (Chrome, Firefox, Edge, etc.)
- It sends an HTTP request to the web server
- Example requests: Submitting a form, clicking a link, and sending JSON data.
2. Web Server
- The web server receives client requests
- It forwards dynamic requests (Servlet/JSP) to the web container
- Handles static resources like HTML, CSS, JS, and images.
- Examples of Web Servers: Apache Tomcat, Jetty.
3. Web Container (Servlet Container)
The web container is responsible for managing servlets. Responsibilities of Web Container
- Loads and initializes servlets
- Manages servlet lifecycle
- Maps URLs to servlets
- Handles multithreading
- Provides request and response objects
- Manages security and sessions
4. Servlet
A servlet is the core component that:
- Processes client requests
- Contains business logic
- Generates responses (HTML, JSON, XML)
Servlets typically extend:
HttpServlet
5. Request and Response Objects
1. HttpServletRequest:
- Holds client request data
- Parameters, headers, cookies, session data
2. HttpServletResponse:
- Used to send response back to client
- HTML, JSON, status codes, headers
Related Article: Servlet Lifecycle
Example of Servlet Architecture
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void init() throws ServletException {
// Initialization code
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Hello, Welcome to Servlet Architecture!</h2>");
}
public void destroy() {
// Cleanup code
}
}
Code Explanation:
- The browser sends a request to HelloServlet.
- The web container loads and initializes the servlet.
- The doGet() method processes the request.
- The servlet generates an HTML response.
- The response is sent back to the browser.