
June 11, 2025
Building Your First Servlet: Handling HTTP Requests in Java
Have you wondered how Java web apps handle user requests? HTTP requests like login forms, product searches, and dynamic data retrieval need servlets. When I initially began Java web programming, servlets were frightening, but after building my first one, everything made clear. Build a simple servlet that supports GET and POST requests using this tutorial. A deployable, testable sample will result.
Setting Up a Servlet in Java
First, we need to prepare our work setup before we start coding. You will need the JDK, an IDE such as IntelliJ IDEA or Eclipse, and Apache Tomcat to run your servlet application. Adding Maven dependency simplifies things. Annotations replace web.xml in most Java web applications.
When your project is ready, make sure to include javax.servlet-api in your dependencies. If you are using Maven, include this in your pom.xml file:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
Now that we are ready, let's make our first servlet.
Creating a Simple Servlet
Servlets are Java classes that utilize HttpServlet and modify HTTP request methods. Here's a simple example:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, Servlet!</h1>");
}
}
This simple servlet waits for HTTP GET requests at the /hello address and replies with an HTML message. Let's take a closer look at how GET and POST work.
Handling GET Requests
GET requests are often used to get info. In this case, we can change the doGet() method to accept query parameters:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
if (name == null) {
name = "Guest";
}
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, " + name + "!</h1>");
}
So, if you go to http://localhost:8080/hello?name=John, you will see "Hello, John!" as the response.If you do not give a name, it will default to "Guest."
Handling POST Requests
To transmit data, such when you submit a form, you utilize a POST request. We can switch our computer to handle form entries.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html");
response.getWriter().println("<h1>Welcome, " + username + "!</h1>");
}
To test this, create a simple HTML form:
<form action="hello" method="post">
<input type="text" name="username" placeholder="Enter your name" required>
<input type="password" name="password" placeholder="Enter your password" required>
<button type="submit">Submit</button>
</form>
The servlet handles form submissions and delivers customized responses.
Deploying and Testing the Servlet
Launch your project in Tomcat to view everything. Launch Tomcat, visit the /hello endpoint for GET requests, then submit the form to test POST requests. Testing request types using Postman is also possible.
Conclusion
Your first servlet works! You learned how to put up a servlet, process GET and POST requests, and deploy it. Session management, database integration, and advanced servlet features await you. Now try your own servlet apps!
61 views