Before Spring MVC, Spring Boot, or Jakarta REST, Java web applications were built directly on Servlets.
Even today, understanding servlets helps developers grasp how web frameworks actually work under the hood.
Servlets are part of the Jakarta Servlet API, which powers most Java web frameworks.
📌 TLDR
Servlets are the core Java web technology that handles HTTP requests and responses. They provide mechanisms for routing requests, managing sessions, filtering traffic, and reacting to application events.
Most modern frameworks (Spring MVC, Spring Boot) are built on top of this model.

🔸 WRITING A SERVLET
A Servlet is a Java class that handles HTTP requests and produces responses.
You typically extend HttpServlet and override methods like doGet() or doPost().
- ▪️ The servlet container (Tomcat, Jetty, GlassFish) manages the lifecycle
- ▪️ Requests are mapped to servlets via annotations or configuration
- ▪️ The servlet generates the HTTP response
🔸 REQUEST FORWARDING VS RESPONSE REDIRECTION
Servlets often delegate processing or redirect the client.
Request Forwarding (server-side)
- ▪️ Happens inside the server
- ▪️ Client URL does not change
- ▪️ Request attributes are preserved
Response Redirection (client-side)
- ▪️ Sends HTTP 302 response to the browser
- ▪️ Browser performs a new request
- ▪️ URL changes in the browser
🔸 PERSISTING DATA ACROSS REQUESTS
HTTP is stateless, so servlets provide mechanisms to maintain state.
Session example
Common persistence scopes:
- ▪️ Request scope → valid for a single request
- ▪️ Session scope → per user session
- ▪️ Application scope → shared across the application
🔸 SERVLET FILTERS
Filters intercept requests and responses before they reach servlets.
Typical use cases:
- ▪️ Authentication
- ▪️ Logging
- ▪️ Compression
- ▪️ CORS handling
Example:
🔸 SERVLET LISTENERS
Listeners react to lifecycle events in the web application.
Common listener types:
- ▪️ ServletContextListener → application startup/shutdown
- ▪️ HttpSessionListener → session creation/destruction
- ▪️ ServletRequestListener → request lifecycle
Example:
✅ TAKEAWAYS
- ▪️ Servlets are the foundation of Java web frameworks
- ▪️ HttpServlet processes HTTP requests via doGet, doPost, etc.
- ▪️ Forwarding vs Redirecting controls request flow
- ▪️ Sessions help maintain state in stateless HTTP
- ▪️ Filters and Listeners extend request processing and lifecycle management
💬 “The purpose of abstraction is not to be vague, but to create a new semantic level.” — Edsger W. Dijkstra
Understanding Servlets gives you that deeper semantic level beneath modern frameworks.
#Java #JakartaEE #Servlets #WebDevelopment #Backend #JavaDeveloper #SoftwareArchitecture #SpringFramework #Programming
Go further with Java certification:
Java👇
Spring👇
SpringBook👇
JavaBook👇