Developing Web applications with Tomcat and Eclipse

Apache Tomcat & Eclipse Platform make a great Web development platform

Using Eclipse as a Web development platform is even easier when you integrate it directly with Apache Tomcat. Learn how to set up Eclipse and Apache Tomcat V5.5 to develop the quintessential Hello World servlet.

Nathan A. Good, Author and Software Engineer, Freelance Developer

Nathan A. Good lives in the Twin Cities area in Minnesota. Professionally, he does software development, software architecture, and systems administration. When he's not writing software, he enjoys building PCs and servers, reading about and working with new technologies, and trying to get his friends to make the move to open source software. He's written and co-written many books and articles, including Professional Red Hat Enterprise Linux 3, Regular Expression Recipes: A Problem-Solution Approach, Regular Expression Recipes for Windows Developers: A Problem-Solution Approach, PHP 5 Recipes: A Problem-Solution Approach, and his latest: Foundations of PEAR: Rapid PHP Development.

08 May 2007

Also available in Chinese Japanese Portuguese

Editor's note: This is a sequel to "Using Eclipse as a development environment with Jakarta Tomcat," written by Geoffrey Duck in May 2004.

Required components

The Eclipse V3.2 Callisto integrated development environment (IDE) includes tools for doing Web development and integrating with servers. So, aside from the software development kit (SDK), you need only Eclipse and Apache Tomcat installed. The versions featured in this article are listed below.

Table 1. Components and versions used
ComponentVersion used in this article
Eclipse Platform3.2.2
Java™ EE 5 SDK1.5.0_09
Apache Tomcat5.5.20

The Eclipse IDE is an open source IDE used as the development environment for your JavaServer Pages (JSPs) and Java files. With the Web and Java 2 Platform, Enterprise Edition (J2EE) Development tools installed, Eclipse is great tool for creating HTML, JSPs, and servlets.

You need the Java SDK to run servlets on your machine. Finally, Apache Tomcat is an open source Web and servlet container, used in the official reference implementations for Java Servlet and Java Server Pages.


Installation of Eclipse and Tomcat is similar — both are extracted from the archive file format into a folder on your hard disk. If you ever want to uninstall either, it's as simple as deleting the directory in which you extracted the files.

Install Eclipse

To install the Eclipse IDE, unpack the compressed (.zip or .tar.gz) file you downloaded from Eclipse, then move the unpacked folder into a convenient location. I try to install it in a location that follows the conventions of whatever operating system I'm using. If I'm installing Eclipse on a Macintosh, I put the Eclipse folder in the Applications folder. If I'm using Microsoft® Windows®, I put the unpacked folder into the C:\Program Files directory.

Once you've downloaded and installed Eclipse, start it up. Before you can create a Web project, you need to install the J2EE and Web Development plug-ins. Fortunately, it's easy to install the plug-in using the Callisto Discovery Site.

Install the Web tools

With the Eclipse IDE running, select Help > Software Updates > Find and Install. This option lets you download and install the Web tools without going to a Web site.

Select Search for New Features to Install, as shown below, then click Next.

Figure 1. Install/Update window
Install/Update window

Select Callisto Discovery Site, as shown below, then click Next.

Figure 2. Select the Callisto Discovery Site
Select the Callisto Discovery Site

Under Callisto Discovery Site, check the Web and J2EE Development box. The wizard immediately warns you that you're missing dependencies. Click Select Required, and these warnings should disappear. Some or all of the components in the Graphical Editors and Frameworks and Models and Model Development categories are selected (as shown in Figure 3) because they're required for the installation of the Web and J2EE plug-ins.

Figure 3. Select features for installation
Select features for installation

Click Next to continue, and license agreements appear for each feature you've selected. If you agree with the licenses, accept them and follow the wizard through the rest of the process. When the IDE is finished installing the plug-ins, it asks you to restart Eclipse in order for the changes to take effect. Be sure you restart the IDE before continuing.

After you restart the IDE, the new features are available to begin developing dynamic Web projects.

Install Tomcat

To install Apache Tomcat, extract the files from the downloaded archive and place them into a directory. I put them in my C:\apps directory to make them easy to locate later. That's it for now; use Eclipse as shown later to start Tomcat.

Create a new Web project

From the Eclipse IDE, select File > New > Project to view the project wizards. Type Web in the Wizards box, and the New Project window filters the wizards to show the ones that match. This is an easy way to locate the wizard you want instead of going through each item.

Choose Dynamic Web Project from the list. You use the Standard Web Project Wizard to make static HTML Web projects. You can technically use Tomcat to run static Web sites, although a Web server such as Apache Web Server may be a better choice for static content. Web servers built specifically for serving up static pages tend to have less overhead and are tuned for that purpose.

Target a runtime

Under Target Runtime, you see <None>, as shown in Figure 4, because you haven't created a runtime yet for Apache Tomcat. Click New to open the New Target Runtime Wizard. Select Apache Tomcat V5.5 from the Apache folder, as shown in Figure 5, then click Next.

Figure 4. Create a new dynamic Web project
Create a new dynamic Web project
Figure 5. Create a new server runtime
Create a new server runtime

Now you have the opportunity to name your runtime, although the default name Apache Tomcat V5.5 is fine. Click Browse to locate the base folder for your Apache Tomcat configuration (mine is C:\apps\apache-tomcat-5.5.20, as shown in Figure 6). I recommend leaving the IBM® Java Runtime Environment (JRE) version the same as the workbench default JRE, unless you have a specific reason to change it. Once you've entered a valid directory, click Finish to create the runtime configuration.

Figure 6. Define the server location
Define the server location

Runtime configuration

If you decide later that you want to change your runtime configuration, you can do so by selecting Window > Preferences. Type Runtime in the filter box, and the list shows an Installed Runtimes configuration setting under the Server category. Select your Apache Tomcat runtime, and click Edit to change the name, location of the directory, or JRE version.

You're asked if you want to switch to the J2EE Perspective. I always answer yes because that perspective includes views like the Servers view, which help to start and stop Tomcat later. You can always open the Servers view by selecting Window > Show View.

Add the server

In the Servers view is a view that contains nothing. This view is for application and Web servers; it lets you control them and monitor their status. To control Tomcat directly from the comfort of the IDE, add a reference to Tomcat by selecting New > Server from the context-sensitive menu. The New Server wizard, as shown in Figure 7, will default to the Apache Tomcat V5.5 runtime you just created.

Figure 7. Define a new server
Define a new server

Click Next. You're asked whether you want to add projects to the server. Select the project you created in the previous steps, then click Add.

Figure 8. Add the projects to the server
Add the projects to the server

Click Finish to close the wizard. Now the Servers view contains the Tomcat V5.5 Server with the project under it.

Figure 9. The Tomcat server in the Servers view
The Tomcat server in the Servers view

Create a sample page

The easiest way to test your new dynamic Web project and Tomcat server integration is to create a simple JSP and deploy it to Tomcat. You can create the new JSP file by selecting File > New > Other, or you can use the context-sensitive menu by right-clicking the project name in the Project Explorer view and selecting New > JSP.

Make sure the parent folder is WebContent and name the file index.jsp. Click Finish to create the page using the default template. If you click Next, you can choose different templates to use (such as HTML or XHTML) for the new page. The defaults are fine for this test.

The page code shown below displays a simple date so you can see it working.

Listing 1. Contents of index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Title</title>
<% java.util.Date d = new java.util.Date(); %>
Today's date is <%= d.toString() %> and this jsp page worked!

Deploy the page

Now that the test page is complete, use Eclipse to redeploy it to Apache Tomcat. In the Servers view, notice that State is set to Republish. That is because changes have been made since the project was last deployed to the server. Right-click the server and select Publish from the menu. The state changes to Synchronized once the project has been deployed to the server.


From time to time, things may go wrong. To view errors that occur from Tomcat, use the Console view. The Console captures the output from the servers you have set up in your Servers view.

With the project successfully deployed and the server started, open a browser and navigate to http://localhost:8080/[ProjectName]/ (where [ProjectName] is the name of the dynamic Web project you created). The new Web page you created appears, and the date should be current. If you refresh the page, the date changes.

Figure 10. Output of index.jsp in a browser
Output of index.jsp in a browser

Hello World servlet

You can add Java servlets to your project and deploy them. To create a quick servlet you can use for testing purposes, use the Servlet Wizard by right-clicking your dynamic Web project in the Project Explorer view and selecting New > Other. Under the Web folder, select Servlet, then click Next.

Add the package name com.example.servlets next to Java Package and add HelloServlet next to Class name. Click Finish. This is an example, so the defaults are fine. The Eclipse IDE generates a servlet class for you. To watch the test servlet in action, add a line in the doGet() method to print a message to the ResponseWriter. The servlet class should look like the one shown below.

Listing 2. Contents of
package com.example.servlets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * Servlet implementation class for Servlet: HelloServlet
 public class HelloServlet extends javax.servlet.http.HttpServlet 
     implements javax.servlet.Servlet {

    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	public HelloServlet() {
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, 
	     HttpServletResponse response)
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
	    throws ServletException, IOException {
		response.getWriter().write("Hello, world!");
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, 
	     HttpServletResponse response)
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
	    throws ServletException, IOException {
		// TODO Auto-generated method stub

After you add the servlet, the Eclipse IDE notes that your project has changed. Look again at the Servers view; Restart now appears in the Status column next to the Tomcat server. To restart the server, right-click the server in the Servers view, then select Restart > Start. Open a browser and navigate to http://localhost:8080/[ProjectName]/HelloServlet (where [ProjectName] is the name of your dynamic Web project). The text "Hello, world!" appears in the browser window. You can add the date, similar to the code in the index.jsp file, to the doGet() method, and redeploy the servlet using the Eclipse IDE.


It's productive to develop JSPs and servlets without having to leave the IDE to start, stop, or deploy to your server. The combination of Eclipse and Apache Tomcat provides an easy integrated environment that enables you to speed up development.



Get products and technologies


  • The Eclipse Platform newsgroups should be your first stop to discuss questions regarding Eclipse. (Selecting this will launch your default Usenet news reader application and open eclipse.platform.)
  • The Eclipse newsgroups has many resources for people interested in using and extending Eclipse.
  • Participate in developerWorks blogs and get involved in the developerWorks community.


developerWorks: Sign in

Required fields are indicated with an asterisk (*).

Need an IBM ID?
Forgot your IBM ID?

Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.


All information submitted is secure.

Dig deeper into Open source on developerWorks

Zone=Open source
ArticleTitle=Developing Web applications with Tomcat and Eclipse