Creating Servlet Example in Eclipse

Last Updated : 7 May, 2026

Servlets are server-side Java programs used to create dynamic web applications by handling client requests and generating responses. They act as a bridge between web requests and backend processing, making them an important part of Java web development.

  • Helps process client requests and generate dynamic web responses.
  • Provides integration between web servers and Java applications.
  • Commonly used in Java-based web applications for handling HTTP requests and responses.

Prerequisites

  • Java 8 or above
  • Eclipse IDE
  • Apache Tomcat Server

Steps to Create a Servlet

Follow the below steps to create and run a basic Servlet example in Eclipse IDE using Apache Tomcat server.

Step 1: Create a Dynamic Web Project

In Eclipse, go to File -> New -> Dynamic Web Project and click on it.

Step 2: Select Project Configuration

Enter the project name, verify the project location and runtime settings, then click on Next to continue.

The source folders on the build path and the classes folder will be displayed here. Click on Next.

This step creates the web module where HTML and JSP files are stored. Select the web.xml checkbox to generate the deployment descriptor file, then click on Finish.

Now by Following above steps Finally the project structure should be created.

Project Structure

Step 3: Add servlet-api.jar file 

Add the Servlet API library required for Servlet development.

Note: If the servlet-api.jar file is not available with your server, download it from Maven Repository and add it as an external JAR to the project.

  • Right-click on the project.
  • Go to Build Path -> Configure Build Path.

Open the Libraries tab. and Click on Add External JARs.

Once the jar file is added, click on Apply and Close. The added jar file will be visible under the lib folder in your project.

Step 4: Create Servlet Class

To create a Servlet, go to folder src -> New -> Servlet.

If the Servlet option is not there, go to Other and search for Servlet and fill the details and make javax.servlet.http.HttpServlet class as superclass.

Specify the URL mapping for the Servlet, modify the default mapping if needed, and click on Next.

HTTPServlet provides methods like init() for initialization and doGet() for handling GET requests. Select the doGet() checkbox to display a Welcome message, then click Finish.

Step 5: Implement the Logic

In the doGet() method, implement the logic to display the welcome message to the user.

HelloServlet.java:

Java
package com.welcome;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

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

        response.setContentType("text/html");
        
        PrintWriter out = response.getWriter();
        
        out.print("<html><body>");
        out.print("<h2>Welcome to GeeksForGeeks</h2>");
        out.print("</body></html>");
    
    }
}

Step 6: Run the Project

Right-click on the HelloServlet.java class, Run As -> Run on Server.

If we want to Debug the servlet, you can click on Debug also here.

Make sure the Tomcat server is configured properly on localhost and click on Next.

Check the Project you created is in configured section and then click on Finish. If the project is showing in the Available section, select the project and click on Add to configure the project on the server.

Output:

Explanation: The servlet is successfully executed on the Apache Tomcat server, and the browser displays the “Welcome to GeeksForGeeks” response using the mapped URL pattern /HelloServlet.

Comment