JSP vs Servlet
Java Servlets are a fundamental technology in Java-based web development.
Before modern frameworks such as Spring MVC and Spring Boot became popular, servlets formed the backbone of server-side Java web applications.
Even today, understanding servlets is essential because many higher-level frameworks are built on top of servlet technology.
A Servlet is a Java class that runs on a web server and handles client requests, usually from a web browser.
Servlets are used to process HTTP requests and generate dynamic responses, such as HTML pages or JSON data.
Servlets operate inside a Servlet Container (also known as a web container), which manages their lifecycle, request handling, and security.
Popular servlet containers include Apache Tomcat, Jetty, and GlassFish.
In simple terms:
The browser sends an HTTP request
The servlet processes the request
The servlet generates a response
The server sends the response back to the client
Before servlets, web servers mainly served static HTML pages.
Servlets introduced dynamic content generation, allowing web applications to:
Process form data
Access databases
Manage user sessions
Generate dynamic HTML or API responses
Servlets bridge the gap between HTTP requests and Java business logic.
Servlets are written in Java, making them platform-independent.
They can run on any operating system that supports a Java Virtual Machine (JVM).
Unlike CGI scripts that create a new process for each request, servlets use a multi-threaded approach, making them faster and more efficient.
Servlets benefit from Java’s built-in security features, such as memory management and exception handling.
Servlet containers efficiently manage multiple requests, allowing applications to scale to handle many users.
Servlets integrate easily with databases, APIs, and enterprise Java libraries.
Servlet code can become lengthy and difficult to manage, especially when generating HTML directly.
Mixing business logic and presentation logic in servlets can make applications harder to maintain.
Beginners may find servlet APIs complex compared to modern frameworks.
Servlets are not designed for frontend rendering, which is why technologies like JSP and templating engines are often used alongside them.
The core components of servlet architecture include:
Client (Web Browser)
Web Server
Servlet Container
Servlet
The servlet container is responsible for:
Loading servlets
Managing servlet lifecycle
Handling multithreading
Mapping URLs to servlets
Most servlets extend the HttpServlet class, which provides methods for handling HTTP requests.
doGet() – Handles HTTP GET requests
doPost() – Handles HTTP POST requests
doPut() – Handles HTTP PUT requests
doDelete() – Handles HTTP DELETE requests
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/example")
public class ExampleServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.getWriter().println("Hello from Servlet!");
}
}
The servlet lifecycle is managed entirely by the servlet container.
The servlet container loads the servlet class and creates an instance when the application starts or when the first request arrives.
init() method)The init() method is called once to initialize resources.
public void init() {
System.out.println("Servlet initialized");
}
service() method)The container calls the service() method for each request. This method dispatches requests to doGet(), doPost(), and other HTTP methods.
destroy() method)The destroy() method is called when the servlet is removed from service.
public void destroy() {
System.out.println("Servlet destroyed");
}
Use an IDE like Eclipse and configure Apache Tomcat.
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, Servlet World!</h1>");
}
}
Deploy the project on Tomcat and open:
http://localhost:8080/your-project-name/hello
You should see a dynamic message generated by the servlet.
Servlets: Low-level, powerful, but verbose
JSP: Better for view rendering
Spring MVC / Spring Boot: Built on servlets, easier to use, cleaner architecture
Understanding servlets helps developers grasp how modern Java web frameworks work internally.
Avoid writing HTML directly in servlets
Use MVC architecture
Handle exceptions properly
Manage resources efficiently
Keep servlets lightweight and focused
Java servlets are the foundation of Java web development.
Although modern frameworks have simplified development, servlets remain an essential concept for understanding how Java web applications process requests and responses.
By learning servlets, developers gain deeper insight into request handling, lifecycle management, and server-side programming principles.
This knowledge not only strengthens Java fundamentals but also makes it easier to master advanced frameworks and enterprise-level systems.