VOOZH about

URL: https://www.geeksforgeeks.org/java/creating-servlet-example-in-eclipse/

⇱ Creating Servlet Example in Eclipse - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

πŸ‘ Image

Step 2: Select Project Configuration

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

πŸ‘ Image

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

πŸ‘ Image

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.

πŸ‘ Image

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

πŸ‘ Image
Project Structure

Step 3: Add servlet-api.jar file 

Add the Servlet API library required for Servlet development.

πŸ‘ Image

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.
πŸ‘ Image

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

πŸ‘ Image

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.

πŸ‘ Image

Step 4: Create Servlet Class

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

πŸ‘ Image

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.

πŸ‘ Image

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

πŸ‘ Image

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.

πŸ‘ Image

Step 5: Implement the Logic

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

HelloServlet.java:

Step 6: Run the Project

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

πŸ‘ Image

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

πŸ‘ Image

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

πŸ‘ Image

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:

πŸ‘ Image

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
Article Tags:
Article Tags: