How To Create Servlet In Eclipse IDE Using Tomcat Server7


In this article we discuss how to create a servlet in the Eclipse IDE using Tomcat Server 7.

Creating Servlet In Eclipse IDE Using Tomcat 7

  • Create a dynamic web project (like servletapis)
  • Create a Servlet (like
  • add servlet-api.jar file
  • Start tomcat server and deploy the project.

1. Create the dynamic web project

For creating a dynamic web project click on file menu then select "New" -> "dynamic web project". A new window opens asking for the project name. For my project I used "servletapis".
Now click on "Finish". 

2. Create the servlet in the Eclipse IDE

For creating a servlet explore the project by clicking the "+" icon then select "explore the Java resources" then right-click on "src" then select "New" -> "Servlet". A window will be generated as in the following.
Now in the Java package write our package name (like mypack) and in the class name write our class name (like Helloworld) as in the following:
Click on "Finish", now a window is generated with some default commands as in the following:
Now change the as in the following and save the code in
  1. package mypack;  
  2. import;  
  3. import;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.annotation.WebServlet;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. @WebServlet("/Helloworld")  
  10. public class Helloworld extends HttpServlet {  
  11.  private static final long serialVersionUID = 1 L;  
  12.  public Helloworld() {  
  13.   super();  
  14.  }  
  15.  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  16.   response.setContentType("text/html");  
  17.   PrintWriter out = response.getWriter();  
  18.   out.print("<html><body>");  
  19.   out.print("<h3>Welcome To Servlet</h3>");  
  20.   out.print("</body></html>");  
  21.  }  
  22. }  

3. Add a JAR file in the Eclipse IDE

For adding a JAR file right-click on our project then select "Build Path" -> "Configure Build Path" then click on the libraries tab in the Java build path. The following window should appear.
Now click on the "Add External JARs" button then select the servlet-api.jar file under the tomcat/lib as in the following:
Now click on "Open" then on "OK". Now we will see the error is removed from our file. Now our servlet has been created.

4. Start the server and deploy the project.

For starting the server and deploying the project in one step right-click on our project then select "Run As" -> "Run on Server" then choose "Tomcat Server".
Now click on "Next" -> "addAll" -> "Finish".
Now a window appears and shows the following message ("welcome to servlet"). If you receive the same message then you have done it successfully.