How To Create Servlet In Eclipse IDE Using Tomcat Server7

Introduction

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 Helloworld.java)
  • 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".

fig-1.jpg

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.

fig-2.jpg

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:

fig-3.jpg

Click on "Finish", now a window is generated with some default commands as in the following:

fig-4.jpg

Now change the Helloworld.java as in the following and save the code in Helloworld.java.

Helloworld.java

package mypack;

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;

/**

 * Servlet implementation class Helloworld

 */

@WebServlet("/Helloworld")

public class Helloworld extends HttpServlet

  {

    private static final long serialVersionUID = 1L;      

    /**

    * @see HttpServlet#HttpServlet()

    */

    public Helloworld()

      {

        super();

        // TODO Auto-generated constructor stub

      }

    /**

    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

    */

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

    // TODO Auto-generated method stub

    response.setContentType("text/html");

    PrintWriter out=response.getWriter();          

    out.print("<html><body>");

    out.print("<h3>Welcome To Servlet</h3>");

    out.print("</body></html>");

  }     

}

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.

Fig-5.jpg

Now click on the "Add external JARs" button then select the servlet-api.jar file under the tomcat/lib as in the following:

Fig-6.jpg

Now click on "Open" then on "OK". Now we will see the error is removed from our Helloworld.java 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".

Fig-7.jpg

Now click on "Next" -> "addAll" -> "Finish".

Now a window appears and shows the following message ("welcome to servlet"). If you recieve the same message then you have done it successfully.

Fig-8.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all