Servlet API Overview in Java – Beginner’s Complete Guide

Introduction

When you learn Servlets, you often hear the term Servlet API.
Understanding the Servlet API is essential, because it provides the rules, classes, and methods that allow Servlets to work with web servers.

This guide explains the Servlet API from scratch, using simple language, clear structure, and beginner-friendly examples—so it can fully replace a live classroom explanation.

What Is the Servlet API?

Definition:

The Servlet API is a collection of Java interfaces and classes that define how Servlets interact with web servers and clients.

 In simple words:

The Servlet API is a toolkit that helps Java programs handle web requests and responses.

Why Do We Need the Servlet API?

Without the Servlet API:

  • Java would not understand HTTP requests

  • Servers could not communicate with Java code

  • Web applications could not be built using Java

The Servlet API helps to:

  • Read data from browser requests

  • Send responses back to users

  • Manage sessions

  • Control servlet lifecycle

  • Integrate with servers like Tomcat

Every Java Servlet is built using the Servlet API.

Who Provides the Servlet API?

The Servlet API is:

  • Defined by Jakarta EE (formerly Java EE)

  • Implemented by servlet containers like:

    • Apache Tomcat

    • Jetty

    • GlassFish

Developers use the API
Servers implement the API

Where Does the Servlet API Fit in Java Web Architecture?

Browser
   ↓
Servlet Container (Tomcat)
   ↓
Servlet API
   ↓
Your Servlet Code

The Servlet API acts as a bridge between:

  • Web server

  • Java servlet code

Main Packages in the Servlet API

The Servlet API is mainly divided into two packages:

 

jakarta.servlet

Used for generic servlet functionality

 

jakarta.servlet.http

Used specifically for HTTP-based web applications

 

Most web applications use the http package.

Core Interfaces of the Servlet API

Servlet Interface

  • The root interface of all servlets

  • Defines lifecycle methods:

    • init()

    • service()

    • destroy()

Rarely implemented directly by beginners.

HttpServletRequest

Used to:

  • Read request data

  • Access form parameters

  • Read headers

  • Get session information

Example:

String name = request.getParameter("username");

HttpServletResponse

Used to:

  • Send data back to browser

  • Set response type

  • Redirect requests

Example:

response.setContentType("text/html");

HttpSession

Used to:

  • Store user data across requests

  • Maintain login state

Example:

HttpSession session = request.getSession();

Important Classes in the Servlet API

HttpServlet Class

  • Most commonly used servlet class

  • Implements Servlet interface

  • Provides methods like:

    • doGet()

    • doPost()

Beginners almost always extend HttpServlet.

ServletConfig

  • Provides servlet-specific configuration

  • Reads init parameters from web.xml

ServletContext

  • Shared across entire application

  • Used for global configuration and resources

Servlet API Lifecycle Support

The Servlet API defines lifecycle behavior:

MethodPurpose
init()Initialize servlet
service()Handle requests
destroy()Cleanup resources

The container follows this API strictly.

Example: Servlet API in Action

protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
                     throws IOException {

    response.getWriter().println("Servlet API is working!");
}

Explanation:

  • HttpServletRequest → Reads request

  • HttpServletResponse → Sends response

  • Provided by Servlet API

Real-Life Analogy

Think of Traffic Rules 

  • Servlet API → Traffic rules

  • Tomcat → Traffic police

  • Servlet code → Drivers

Everyone must follow the same rules for smooth operation.

Servlet API vs Servlet Container

Servlet APIServlet Container
Set of interfaces & classesSoftware that runs servlets
Defines rulesImplements rules
Used by developersUsed by servers
Example: HttpServletExample: Tomcat

Best Practices for Beginners

  • Extend HttpServlet, not Servlet
  • Learn core interfaces first
  • Understand request & response objects
  • Use API documentation
  • Practice with small examples