Creating Servlet Example in Eclipse
Last Updated :
14 Apr, 2025
Servlets are Java programs that run on a Java-enabled web server or application server. They are used to handle the request obtained from the web server, process the request, produce the response, and then send the response back to the web server.
Prerequisites:
Once Eclipse is installed and configured with the Tomcat server, below are the steps to create a basic Servlet in Eclipse IDE.
Steps to Create a Servlet
In this example, we will create a basic servlet that displays a Welcome message to the user in the browser.
Step 1: Create a Dynamic Web Project
In Eclipse, go to File -> New -> Dynamic Web Project and click on it.

After clicking on Dynamic web project, the below window will open to enter the required project details.

- Enter the Project Name.
- Check if the location where the project saves is correct.
- Check if the Run time selected for the project is displaying. If you want to change any of the configurations for runtime, you can do by clicking Modify.
- Click on Next.

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

- This is the web module creation, where all your HTML and JSP pages will be added.
- We can also generate a Deployment Descriptor - web.xml file to define the mappings between URL paths and the servlets to handle the requests with those paths. This can also be done using @WebServlet() annotation in the Servlet class.
- In this example, we will be using annotation in our servlet class to map the URL path.
- But for the demonstration purpose to show the web.xml file creation in the project, we will select the web.xml checkbox also.
- Click on Finish.
Project StructureIn this way, the project structure should be created.
Step 2: servlet-api.jar file
As we are working with servlets, we need to have the servlet-api.jar file in our project. This jar is a library that contains all the interfaces and classes of the Servlet API, so we can use its functionality to develop our web application.
- In this example, we are using the Apache Tomcat server to run the project.
- Tomcat container is an open-source Java servlet container that implements several core Java enterprise functionalities like the Java Servlet, JSP, etc., so it provides this servlet-api.jar file by default.
- You can check the jar file in the below path if you are using the Tomcat server only.

As we can see, under Apache Tomcat, there is a servlet-api.jar file by default.
For Different servers: In case, if you are using a different server and the servlet-api.jar file is not there, you can download it from Maven Repository. Add the downloaded jar file as an external jar to your project like below:

Go to the project name and right-click on it. Go to Build Path -> Configure Build Path.

- In this window, it will show all the libraries that are associated with the project, and also you can add any required jar files to your project.
- Go to the Libraries tab and click on Add External JARs.
- Select the servlet-api.jar file from the location you downloaded and add.

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 3: 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.

- It is a good practice to create Java classes inside Packages.
- Enter the package name and Class name.
- Java Servlets are available under javax.servlet.http.HttpServlet. So the Superclass is HttpServlet.
- Click on Next.

- As we learned about the URL mappings while creating the project, we can specify those URL mapping for the particular servlet here.
- The URL specified here should be used in mapping the servlets either in web.xml or by using annotation.
- It will show the default mapping name but if you want, you can change that by selecting the URL and clicking on Edit.
- Click on Next.

- HTTP Servlet is a protocol-specific Servlet and it provides all these methods that are shown in the above screenshot.
- We can use these methods based on our requirements.
- For example, if we want to implement logic while initialization of the servlet, we can override the init() method.
- For HTTP GET requests, to get the information for the request, we can use the doGet() method.
- Get more information on HTTP Servlet methods here.
- In this example, we are just showing a Welcome message to the user. So, select the doGet Checkbox.
- Click on Finish.
HelloServlet.java
- As shown above, it creates the HelloServlet.java Servlet class with the doGet() method we selected.
- As you can see, it automatically provided the @WebServlet("/HelloServlet") annotation for this servlet to map the URL. So, there is no need to enter the mapping again in the web.xml file.
- In Java for every class there will be a default constructor, so that constructor is also auto-generated here.
- If you are aware of the Serialization concept in Java, the serialVersionUID which is a version number associated with every serialization class is also generated while creating a Servlet in Eclipse IDE.
Step 4: 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>");
}
}
- Once we get the Get request from the browser, the doGet() method will be executed.
- Here, we are setting the response content type as "text/html" so that the response that is being sent to the client/browser will be considered as text/html.
- In the java.io package, Java provides a PrintWriter class to print the formatted output to the text-output streams.
- So, get the PrintWriter object and print the Welcome Message.
- We need to add all the required packages in the servlet class.
- As we specified the content type as "text/html", the tags we mentioned in the output stream - out.print() will be considered as the HTML tags and the message "Welcome to GeeksForGeeks" will be written in the response to the browser.
Step 5: 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:
Once the server is started, it will configure and run the HelloServlet.java file on the server.

We can run the URL: http://localhost:8080/JavaServlets/HelloServlet in the browser. Make sure the URL specified in the Servlet "/HelloServlet" is mapped correctly.
Similar Reads
Creating JSP in Eclipse IDE with Tomcat Server
Eclipse IDE is an open-source Integrated Development Environment that is popular for Java application development (Java SE and Java EE) and Android apps. It also supports C/C++, PHP, Python, Perl, and other web project developments via extensible plug-ins. JSP stands for java server pages. It is a s
3 min read
How to Create Servlet in MyEclipse IDE?
Servlets are Java programs that extend the capabilities of a Web Server. It runs on a Java-enabled web server like Apache Tomcat server, to receive and respond to the client's requests. With Servlets, we can create Enterprise-grade applications with database access, session management, and content g
8 min read
Servlet - CRUD Operation with Example
CRUD means Create, Read, Update and Delete. These are the basic important operations carried out on the Database and in applications. We will build a simple User registration application using a Servlet, MYSQL, and JDBC for demonstration. In this example, we will be able to create users, read users,
10 min read
Life Cycle of a Servlet
The entire life cycle of a Servlet is managed by the Servlet container, which uses the jakarta.servlet.Servlet interface to understand the Servlet object and manage it. So, before creating a Servlet object, let's first understand the life cycle of the Servlet object, which is actually understanding
6 min read
Servlet - Fetching Result
Servlet is a simple java program that runs on the server and is capable to handle requests from the client and generate dynamic responses for the client. How to Fetch a Result in Servlet? It is depicted below stepwise as shown below as follows: You can fetch a result of an HTML form inside a Servlet
3 min read
Servlet - Exception Handling
When a servlet throws an exception, the web container looks for a match with the thrown exception type in web.xml configurations that employ the exception-type element. To define the invocation of servlets in response to particular errors or HTTP status codes, you'd have to utilize the error-page el
4 min read
How to Create a Maven Project in Eclipse IDE?
Maven is a powerful project management tool that is based on POM (project object model). It is used for project build, dependency, and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT. In short terms we can tell maven is a tool that can be used for buildi
2 min read
Hibernate Example using XML in Eclipse
Hibernate is a framework that provides some abstraction layer, meaning that the programmer does not have to worry about the implementations, Hibernate does the implementations for you internally like Establishing a connection with the database, writing queries to perform CRUD operations, etc. In thi
6 min read
How to create unit tests in eclipse?
Unit testing, also known as component testing, involves testing small pieces of code to ensure that their actual behavior matches the expected behavior. Eclipse, a widely used IDE for Java development, is also commonly used for testing purposes. In Java, JUnit is the preferred framework for unit tes
6 min read
How to Create Jar File for Java Project in Eclipse?
Java programmers already know about what is a jar file if you are new then first understand what is a jar file. Once you are completed your project in Eclipse the next task is to create a runnable version of your project. Eclipse support only exporting the JAR (.jar) file, not the Executable (.exe)
3 min read