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)
Open Eclipse
Go to
File → New → Dynamic Web ProjectEnter project name:
SimpleServletAppSelect target runtime: Apache Tomcat
Click Finish
Eclipse creates a basic web application structure.
Step 2: Create a Servlet Class
Right-click
srcfolderSelect
New → ServletPackage name:
com.example.servletServlet name:
HelloServletClick 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 servletsdoGet()→ Handles browser GET requestsHttpServletRequest→ Holds request dataHttpServletResponse→ Sends data back to browserPrintWriter→ 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
Open Servers tab
Right-click Tomcat → Add and Remove
Move
SimpleServletAppto ConfiguredClick 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/helloOutput:
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