How to Deploy a Simple Servlet in Apache Tomcat – Beginner Guide

Introduction

After installing Java JDK, IDE (Eclipse/IntelliJ), and Apache Tomcat, the next important step is deploying your first Servlet.

Deploying a servlet means:

Making your Java servlet run on a server so it can be accessed from a web browser.

This lesson explains everything from scratch, so even if you have never deployed a servlet before, you can follow along confidently.

What Does “Deploying a Servlet” Mean?

Simple Definition

Deploying a servlet means:

  • Placing your servlet inside a web application

  • Running it on a Servlet container (Tomcat)

  • Accessing it through a URL in the browser

Once deployed, the servlet behaves like a real web endpoint.

Prerequisites (Before You Start)

Make sure you already have:

  • Java JDK installed

  • Apache Tomcat installed

  • Eclipse or IntelliJ IDEA installed

If Tomcat opens at http://localhost:8080, you are ready.

Creating a Simple Servlet (Step-by-Step)

Step 1: Create a Dynamic Web Project (Eclipse)

  1. Open Eclipse

  2. Go to
    File → New → Dynamic Web Project

  3. Enter project name:
    SimpleServletApp

  4. Select target runtime: Apache Tomcat

  5. Click Finish

Eclipse creates a basic web application structure.

Step 2: Create a Servlet Class

  1. Right-click src folder

  2. Select
    New → Servlet

  3. Package name:
    com.example.servlet

  4. Servlet name:
    HelloServlet

  5. Click Finish

Writing the Simple Servlet Code

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! My First Servlet is Running.</h1>");
    }
}

Understanding the Code 

  • HttpServlet → Base class for servlets

  • doGet() → Handles browser GET requests

  • HttpServletRequest → Holds request data

  • HttpServletResponse → Sends data back to browser

  • PrintWriter → Writes HTML output

This servlet simply displays a message in the browser.

Mapping the Servlet URL

Option 1: Using Annotation

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    ...
}

This means:

http://localhost:8080/SimpleServletApp/hello

Option 2: Using web.xml (Traditional Way)

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.example.servlet.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>

Beginners should prefer annotations.

Deploying Servlet on Apache Tomcat (Eclipse)

Step 1: Add Project to Server

  1. Open Servers tab

  2. Right-click Tomcat → Add and Remove

  3. Move SimpleServletApp to Configured

  4. Click Finish

Step 2: Start Tomcat Server

  • Right-click Tomcat → Start

  • Watch Console for errors

If no error appears, deployment is successful.

Accessing the Servlet in Browser

Open browser and type:

http://localhost:8080/SimpleServletApp/hello

Output:

Hello! My First Servlet is Running.

Congratulations! You have successfully deployed your first servlet.

How Deployment Works (Behind the Scenes)

Browser
   ↓
Tomcat Server
   ↓
Servlet Container
   ↓
HelloServlet
   ↓
HTML Response

Tomcat handles servlet lifecycle automatically.

Real-Life Analogy

Think of a TV Show 

  • Servlet code → Recorded episode

  • Tomcat → TV channel

  • Browser → Viewer

Deployment is like broadcasting the episode so viewers can watch it.

Best Practices for Beginners

  • Use annotations (@WebServlet)
  • Keep servlet logic simple
  • Always check server logs
  • Use meaningful URL patterns
  • Restart server after major changes