What Is a Servlet in Java? Beginner’s Guide to Java Web Development

Introduction

When you browse a website, you send requests to a server, and the server sends responses back to your browser.
In Java web development, this communication is handled using Servlets.

If you are new to Java web technologies, don’t worry—this guide starts from zero, explains everything in simple language, and gradually moves toward technical concepts with clear examples.

What Is a Servlet?

Definition:

A Servlet is a Java program that runs on a web server and handles client requests (usually from a web browser) and returns responses.

 In simple words:

A Servlet acts as a bridge between a user’s browser and Java code running on a server.

Why Are Servlets Used in Web Applications?

Before Servlets, Java programs were mostly standalone applications. But web applications need to:

  • Receive data from users (forms, URLs)

  • Process that data using business logic

  • Generate dynamic responses (HTML, JSON, etc.)

Servlets are used because they:

  • Handle HTTP requests and responses

  • Generate dynamic web content

  • Are platform-independent

  • Are secure, fast, and scalable

  • Integrate well with Java frameworks like JSP, Spring, Hibernate

 Almost every Java web framework internally uses Servlets.

Where Do Servlets Fit in Java Web Architecture?

Java Web Application Architecture

Browser (Client)
     ↓
 Web Server (Tomcat)
     ↓
   Servlet
     ↓
 Business Logic (Java Classes)
     ↓
 Database

Role of Servlets:

  • Act as the controller

  • Receive user input

  • Decide what logic to execute

  • Send the response back to the browser

 This follows the MVC (Model–View–Controller) design pattern.

How Do Servlets Work? (Request–Response Lifecycle)

Servlets follow a request–response model.

 

Step-by-Step Lifecycle:

  1. User enters a URL or submits a form

  2. Browser sends an HTTP request

  3. Web server forwards the request to the Servlet

  4. Servlet processes the request

  5. Servlet generates a response

  6. Server sends the response back to the browser

Text-Based Diagram Explanation

User Browser
     |
     | HTTP Request
     ↓
 Web Server (Tomcat)
     |
     | Calls Servlet
     ↓
   Servlet
     |
     | Processes Data
     ↓
 HTTP Response (HTML/Text)
     |
     ↓
User Browser

Real-Life Analogy

Think of a Restaurant 

  • Customer → User (Browser)

  • Waiter → Servlet

  • Kitchen → Business Logic

  • Food → Response

The customer never goes into the kitchen.
 The waiter (Servlet) takes the order, talks to the kitchen, and delivers food.

Servlet = Waiter of a web application

Basic Servlet Flow

  1. Client sends request

  2. Servlet receives request

  3. Servlet processes request

  4. Servlet sends response

This flow is the core foundation of Java web development.

Beginner-Friendly Servlet Code Example

Step 1: Import Required Packages

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;

Step 2: Create a Servlet Class

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, Welcome to Java Servlets!</h1>");
    }
}

Step-by-Step Explanation

  • HttpServlet → Base class for all Servlets

  • doGet() → Handles GET requests

  • HttpServletRequest → Contains request data

  • HttpServletResponse → Used to send response

  • PrintWriter → Writes output to browser

When the browser hits this servlet URL, it prints a message on the screen.

Servlet Container

Servlets do not run alone. They need a Servlet Container like:

  • Apache Tomcat

  • Jetty

  • GlassFish

What the Container Does:

  • Loads the servlet

  • Manages lifecycle

  • Handles threading

  • Provides security

  • Manages request/response objects

 You never manually create or destroy servlets—the container handles it.

Best Practices for Beginners

  • Keep Servlets small and focused
  • Use Servlets for control logic only
  • Use JSP for UI (View layer)
  • Follow MVC architecture
  • Handle exceptions properly
  • Use meaningful class names