How to Create a Servlet Class Using HttpServlet – Beginner Guide
Introduction
After understanding what a Servlet is and setting up Tomcat, the next important step is creating a Servlet class.
In Java web development, most servlets are created by extending the HttpServlet class.
This lesson explains:
What
HttpServletisWhy we extend it
How to create a servlet class step by step
How browser requests reach your servlet
This guide assumes no prior servlet coding experience.
What Is HttpServlet?
Simple Definition:
HttpServlet is a predefined Java class provided by the Servlet API that helps you handle HTTP requests and responses.
In simple words:
HttpServletgives you ready-made methods to handle browser requests like GET and POST.
Why Do We Extend HttpServlet?
You extend HttpServlet because it already:
Implements the
ServletinterfaceHandles low-level protocol details
Provides easy-to-use methods like:
doGet()doPost()
Beginners should never implement Servlet directly.
Basic Syntax to Create a Servlet Class
public class MyServlet extends HttpServlet {
// Servlet code here
}
Explanation:
extends HttpServlet→ Makes the class a servletYour class now becomes capable of handling HTTP requests
Required Imports (Very Important)
Before writing servlet logic, import required packages:
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.ServletException;
import java.io.IOException;
These classes come from the Servlet API.
Creating Your First Servlet Class
package com.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello! This is my first Servlet.</h1>");
}
}
Understanding Each Part of the Servlet Code
extends HttpServlet
Converts your Java class into a Servlet
Enables HTTP handling
doGet() Method
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
Called when browser sends a GET request
Automatically invoked by Tomcat
You never call it manually
HttpServletRequest
Used to:
Read request data
Access URL parameters
Get session information
Example:
request.getParameter("name");
HttpServletResponse
Used to:
Send data back to browser
Set content type
Redirect requests
Example:
response.setContentType("text/html");
PrintWriter
Used to:
Write output to browser
PrintWriter out = response.getWriter();
out.println("Hello World");
Handling Different HTTP Methods
GET Request
protected void doGet(...) { }
POST Request
protected void doPost(...) { }
Use:
doGet()→ for reading/displaying datadoPost()→ for submitting form data
Mapping the Servlet to a URL
Using Annotation (Recommended)
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
}
Access URL:
http://localhost:8080/YourApp/hello
How the Servlet Class Works Internally
Browser Request
↓
Tomcat Server
↓
HttpServlet
↓
doGet() / doPost()
↓
Response Sent Back
Tomcat controls everything behind the scenes.
Real-Life Analogy
Think of a Reception Desk
Browser → Visitor
HttpServlet → Receptionist
doGet/doPost → Types of requests
Response → Reply given to visitor
The receptionist knows how to handle requests properly.
Best Practices for Beginners
- Always extend
HttpServlet - Keep servlet logic simple
- Use annotations for mapping
- Separate business logic from servlet
- Follow proper naming conventions