Category: java
introduction to servlets and servlet architecture
Published on 30 Sep 2026
Explanation
A Servlet is a Java component that runs on a web server and handles client requests to generate dynamic responses.
Code:
Browser | | HTTP Request v Web Server / Servlet Container | v Servlet | | HTTP Response v Browser
Explanation
The Servlet container such as Tomcat manages the servlet lifecycle, request routing, threading, and communication with the servlet.
Code:
Client | v Apache Tomcat | +--> Servlet 1 +--> Servlet 2 +--> Servlet 3
Explanation
Create a servlet by extending HttpServlet and map it to a URL using the @WebServlet annotation.
Code:
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) {
// Handle request
}
}
Explanation
The servlet container receives the HTTP request and invokes the appropriate servlet method based on the HTTP method and URL.
Code:
GET /hello
|
v
Tomcat
|
v
HelloServlet.doGet()
|
v
HTTP Response
Explanation
Servlets are commonly used as the controller layer in traditional Java web applications.
Code:
Browser | v Servlet Controller | +---- Service | +---- DAO | v Database