table of contents

Using Tomcat with Eclipse

Executive Summary

The time it takes to download an IDE and learn the barebones basics of use will be very quickly recouped by the savings in development, debugging, and deployment times. I personally feel that the extra features of MyEclipse more than justify the very small cost vs. the totally free regular Eclipse IDE described here, but this is definitely a matter of taste, and both versions are widely used. Here is a quick summary of use; see the next sections for details.

  1. Download and install Java from http://java.sun.com/javase/downloads/. I use JDK 1.6.0_10, but any Java 5 or 6 version will work.
  2. Unzip Tomcat. Unzip tomcat-6.0.16-preconfigured.zip into the top level of the C drive.
  3. Install Eclipse. Download from http://www.eclipse.org/downloads/. Choose "Eclipse IDE for Java EE Developers", download, and unzip.
  4. Tell Eclipse about Tomcat. Click on Servers tab at bottom. R-click, New, Server, Apache, Tomcat v6.0, navigate to folder, OK. Copy files from tomcat-dir/conf to workspace/Servers/Tomcat v6.0 Server.
  5. Run Tomcat. Click on Servers tab at bottom. R-click on Tomcat v6.0, choose "Start". Open http://localhost/ in a browser.
  6. Import and Test a Sample Web App. Grab intro-app-eclipse.zip, save it, and import it into Eclipse. Use File, Import, General, Existing Projects, Select archive file. Then click Browse and navigate to intro-app-eclipse.zip. Click on Servers tab at bottom. R-click on Tomcat v6.0 Server, choose "Add and Remove Projects". Choose intro app. Start Tomcat if not already running. Open http://localhost/intro/ in browser.
  7. Create and Test a new Web App. File, New, Project, Web, Dynamic Web Project. Deploy and test it as above.

Unzip Tomcat

Unzip tomcat-6.0.16-preconfigured.zip into the top level of the C drive. This should result in C:\apache-tomcat-6.0.16\. This version of Tomcat has the following settings already in place. For details on customizing this configuration, please see the detailed configuration guide.
  • The port is changed from 8080 to 80. This lets you enter URLs of the form http://localhost/... instead of http://localhost:8080/....
    • When you download Tomcat from the Apache site, the port is 8080 in case you already have another server running on port 80.
  • The invoker servlet is enabled. This lets run servlets with a URL of the form http://localhost/appName/servlet/packageName.servletName. That is, the invoker servlet saves you from editing web.xml to give a servlet-mapping to your servlet.
    • When you download Tomcat from the Apache site, the invoker servlet is disabled. You definitely want the invoker servlet disabled on a server used for a deployed application, but having it enabled on your development server is very convenient for quick testing.
  • Tomcat monitors struts-config.xml and faces-config.xml. Whenever either of these files changes, Tomcat reloads the Web application. This saves you from restarting the server when you change these files.
    • If you do not use Struts or JSF, this change will not be beneficial to you. But it does not hurt either way.
  • Directory listings are turned on. If you type a URL ending in / and there is no welcome file, Tomcat shows a directory listing.
    • Directory listings were on by default in previous Tomcat versions. They are not required but are convenient, especially during development.

Install Eclipse

Go to http://www.eclipse.org/downloads/. Choose "Eclipse IDE for Java EE Developers", download, and unzip. Iinstall with all the default settings. Start it and select "Workbench".

Tell Eclipse about Tomcat

Click on Servers tab at bottom. R-click, New, Server, Apache, Tomcat v6.0, navigate to folder, OK.
Eclipse Tomcat Setup

Unfortunately, Eclipse (unlike MyEclipse) does not copy all of the Tomcat configuration files when you set up a new Tomcat version. So, you have to do this by hand. This step is critical! Copy web.xml, server.xml, and context.xml from tomcat-dir/conf (e.g., C:\tomcat-6.0.16\conf) to workspace/Servers/Tomcat v6.0 Server. You can copy to the filesystem or directly to the Eclipse interface as shown below.
Tomcat Config Files to Copy

Run Tomcat

Click on Servers tab at bottom. R-click on Tomcat v6.0, choose "Start". Open http://localhost/ in a browser: you should see an empty page showing a blank directory listing (but not a 404 error).
Eclipse Running Tomcat

Import a Sample App and Test

Grab intro-app-eclipse.zip, save it, and import it into Eclipse. Use File, Import, General, Existing Projects, Select archive file. Then click Browse and navigate to intro-app-eclipse.zip.
Eclipse Import Project 1
Eclipse Import Project 2
Click on Servers tab at bottom. R-click on Tomcat v6.0 Server, choose "Add and Remove Projects". Choose intro app. Start Tomcat if not already running. Try the following URLs in a browser:

  • http://localhost/intro/ Directory listing showing Hello.html and Hello.jsp. (Unlike with MyEclipse, sometimes you have to restart server after adding a new project. If so, R-click server and choose "Restart".)
  • http://localhost/intro/Hello.html Be sure to use an uppercase "H" in "Hello".
  • http://localhost/intro/Hello.jsp Be sure to use an uppercase "H" in "Hello".
  • http://localhost/intro/servlet/HelloServlet HelloServlet via invoker servlet.
  • http://localhost/intro/servlet/hi HelloServlet via servlet mapping in web.xml.
  • http://localhost/intro/servlet/coreservlets.HelloServlet2 HelloServlet2 via invoker servlet.
  • http://localhost/intro/servlet/hi2 HelloServlet2 via servlet mapping in web.xml.
  • http://localhost/intro/servlet/coreservlets.HelloServlet3 HelloServlet3 via invoker servlet.
  • http://localhost/intro/servlet/hi3 HelloServlet3 via servlet mapping in web.xml.

Making New Apps in Eclipse


  • Make empty project.
    • File, New, Project, Web, Dynamic Web Project.
    • Give it a name (e.g., "test").
    • Accept all other defaults.
Eclipse New Project 1
Eclipse New Project 2

Adding Code to New Apps in Eclipse


  • WebContent.
    Regular Web files (HTML, JavaScript, CSS, JSP, images, etc.)
  • WebContent/some-subdirectory
    Web files in subdirectory.
  • WebRoot/WEB-INF
    web.xml (used for servlet mappings)
  • WebContent/WEB-INF/lib
    JAR files specific to application.
  • Java Resources: src
    Unpackaged Java code.
  • Java Resources: src/somePackage
    Java code in somePackage package.
  • Note:
    You can cut/paste or drag/drop existing files into appropriate locations, but it is hard to drag files into default package until you create at least one class first.
Eclipse New Project 3

Testing New Apps inEclipse

Follow same procedure as given in example above with "intro" app: Click on Servers tab at bottom. R-click on Tomcat v6.0 Server, choose "Add and Remove Projects". Choose app. Start Tomcat if not already running. Open http://localhost/appName/ in browser. (Unlike with MyEclipse, sometimes you have to restart server after adding a new project. If so, R-click server and choose "Restart".)

More Information

Online documentation