Last Updated : 31 Jul, 2025
Java Servlet is a Java program that runs on a Java-enabled web server or application server. It handles client requests, processes them and generates responses dynamically. Servlets are the backbone of many server-side Java applications due to their efficiency and scalability.
Key Features:
Prerequisites
- Install JDK (Java Development Kit)
- Install Apache Tomcat server (e.g., version 9 or 10)
- Install an IDE like Eclipse or IntelliJ IDEA
- Download the Servlet API JAR (already included in Tomcat)
Example: Here's a simple example of how a servlet works:
Step 1: Create a Dynamic Web Project (in Eclipse).
Step 2: Create Servlet class.
HelloWorldServlet.java
Java
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body><h1>Hello, World!</h1></body></html>");
}
}
Explanation:
To deploy a servlet, you need to configure it in the web.xml file. This file maps URLs to servlets. For example,
XML
<web-app xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
Explanation:
This is a web.xml file used for mapping URLs to servlets. So, when you visit http://localhost:8080/yourApp/hello, the servlet runs and shows "Hello, World!" in the browser.
From Servlet 3.0, servlet configuration can also be done using annotations. Instead of using web.xml, we can configure the servlet using the @WebServlet annotations.
Java
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body><h1>Hello, World!</h1></body></html>");
}
}
Explanation: Here we have used the @WebServlet("/hello") annotation to register the servlet directly in code (no need for web.xml). It maps the servlet to the URL /hello.
Why Choose Java Servlet over other Technologies?Dynamic web content requires server-side technologies. While there are many options, Java Servlet stand out due to their advantages over alternatives like Common Gateway Interface (CGI)
Limitations of CGI:
Benefits of Java Servlets:
Java servlets container play a very important role. It is responsible for handling important tasks like load balancing, session management and resource allocation, it make sure that all the requests are process efficiently under high traffic. The container distribute requests accross multiple instances, which helps improve the system performance.
Servlet Architecture can be depicted from the image itself as provided below as follows:
Execution of Java ServletsExecution of Servlets basically involves Six basic steps:
CGI is actually an external application that is written by using any of the programming languages like C or C++ and this is responsible for processing client requests and generating dynamic content.
In CGI application, when a client makes a request to access dynamic Web pages, the Web server performs the following operations:
So, in CGI server has to create and destroy the process for every request. It's easy to understand that this approach is applicable for handling few clients but as the number of clients increases, the workload on the server increases and so the time is taken to process requests increases.
Difference Between Java Servlets and CGIThe table below demonstrates the difference between servlet and CGI
Servlet CGI (Common Gateway Interface) Servlets are portable and efficient. CGI is not portable. In Servlets, sharing data is possible. In CGI, sharing data is not possible. Servlets can directly communicate with the webserver. CGI cannot directly communicate with the webserver. Servlets are less expensive than CGI. CGI is more expensive than Servlets. Servlets can handle the cookies. CGI cannot handle the cookies. Servlets APIs and PackagesServlets are built from two packages:
Various classes and interfaces present in these packages are:
Component Type Package Servlet Interface jakarta.servlet.* ServletRequest Interface jakarta.servlet.* ServletResponse Interface jakarta.servlet.* GenericServlet Class jakarta.servlet.* HttpServlet Class jakarta.servlet.http.* HttpServletRequest Interface jakarta.servlet.http.* HttpServletResponse Interface jakarta.servlet.http.* Filter Interface jakarta.servlet.* ServletConfig Interface jakarta.servlet.* Servlet ContainerServlet container, also known as Servlet engine, is an integrated set of objects that provide a run time environment for Java Servlet components. It is a system that manages Java Servlet components on top of the Web server to handle the Web client requests.
Services provided by the Servlet container:
What is a Servlet Filter?Example : @WebServlet(name = "MyServlet", urlPatterns = "/myServlet")
A Servlet Filter is an additional component for performing pre-post processing work on the web requests like logging, monitoring, debugging and troubleshooting.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4