Saturday, 28 September 2013


How to create a Servlet with Eclipse and Tomcat


Environment Used

  • JDK 6 (Java SE 6)
  • Eclipse Indigo IDE for Java EE Developers (3.7.1)
  • Apache Tomcat 6.x
  • Java EE 5 API (Servlet 2.5)

Setting up development environment

To install and configure Apache Tomcat in Eclipse IDE........

How to configure Apache Tomcat in Eclipse IDE: click here

Project Description

  • This example explains how to develop, deploy and run Servlet in Tomcat using Eclipse IDE.
  • We use Eclipse IDE for Java EE Developers which includes tools for creating Java EE and Web applications.

Creating Dynamic Web Project

To create a Servlet we need to create a new ‘Dynamic Web project’ which can be done in three ways,
  • Right click on Project Explorer -> New -> Dynamic Web Project
  • File menu -> New -> Dynamic Web Project
  • Click on the down arrow on New icon on toolbar -> Dynamic Web Project
Enter the project name as ‘FirstServlet‘ and make sure the Apache Tomcat v6.0 Target Runtime has been selected with the Dynamic web module version as 2.5 and click on ‘Finish‘ button.
You will see the Dynamic web project in the ‘Project Explorer‘ view.

Creating Servlet

  • Right click on src or Project -> New -> Servlet
  • Enter the Java package name as com.theopentutorials.servlets
  • Enter the Class name as HelloWorldServlet
  • Click on ‘Finish
In the doGet() method of your Servlet copy the following code.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.theopentutorials.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class HelloWorldServlet
 */
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public HelloWorldServlet() {
        super();
    }
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
        throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Hello World");
    }
}

Run the Servlet

Method 1:

Deploying Dynamic Web Project
Now we need to deploy the Servlet’s project ‘FirstServlet’ on server.
Deploying the project can be done in two ways,
  • Right click on the ‘FirstServlet’ Dynamic Web project -> Run As -> Run On Server. Select the existing ‘Tomcat v6.0 Server at localhost’ and click Finish.
  • Right click on ‘Tomcat v6.0 Server at localhost’ available in Servers view -> Add and Remove… -> Select the Servlet’s JAR file from the left pane and click Add-> and then Finish.
Start/Restart the Server
Right click on ‘Tomcat v6.0 Server at localhost’ from Servers view and click on Start if it has not yet been started.
Open any browser or Eclipse internal Web Browser and type the following URL for requesting Servlet
http://localhost:8080/FirstServlet/HelloWorldServlet

Method 2

This is simple and easy method and combines the three steps (deploying project, starting server and requesting Servlet) of method 1 in a single step.
You can run the Servlet in many ways,
  • Use Ctrl + F11 to run the Servlet and select the existing Server and Finish.
  • Right click on editor -> Run As-> Run on Server and select the existing Server and Finish.
  • Right click on Servlet file in Project Explorer -> Run As-> Run on Server and select the existing Server and Finish.
Eclipse Internal Web Browser will display the following output