table of contents

Tutorial: Using Tomcat 6 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. To get started with web apps in Eclipse, you only need to know a very small number of features. You can gradually learn the advanced capabilities at your leisure. Here is a quick summary of basic use; see the next sections for details on these core features.

  1. Install Java. Download from http://java.sun.com/javase/downloads/. I use JDK 1.6.0_15, but any Java 5 or 6 version will work. You need the full JDK (with compiler), not just the JRE (for running existing apps).
  2. Unzip Tomcat. Unzip tomcat-6.0.18-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. The latest version as of late 2009 is 3.5 (Galileo), but these directions apply equally to 3.4 (Ganymede).
  4. Tell Eclipse about Tomcat. Click on Servers tab at bottom. R-click, New, Server, Apache, Tomcat v6.0, navigate to Tomcat installation folder (C:\apache-tomcat-6.0.18), OK.
  5. 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). The details below show how to copy the ROOT files that Eclipse erroneously forgets, so that http://localhost/ gives the normal Tomcat welcome page.
  6. Import and Test a Sample Web App. Grab intro.zip, save it, and import it into Eclipse. Use File, Import, General, Existing Projects, Select archive file. Then click Browse and navigate to intro.zip. Click on Servers tab at bottom. R-click on Tomcat v6.0 Server, choose "Add and Remove Projects". Choose intro project. Start Tomcat if not already running. Open http://localhost/intro/ in browser. Note that there are many other sample Eclipse projects to illustrate various servlet and JSP capabilities (session tracking, JSP scripting, MVC, custom tags, and dozens of other topics). Please see http://courses.coreservlets.com/Course-Materials/csajsp2.html. This intro project is merely to confirm that Tomcat and Eclipse are configured properly.
  7. Create and Test a new Web App. File, New, Project, Web, Dynamic Web Project. Deploy and test it as above.

If you find these free tutorials helpful, we would appreciate it if you would link to us. For information on commercial Web hosting providers that support Tomcat, see the JSP hosting page.


Unzip Tomcat

Unzip tomcat-6.0.18-preconfigured.zip into the top level of the C drive. This should result in C:\apache-tomcat-6.0.18\. 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 practice and 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, but are off in the current version. They are convenient during development so you can just click on files, but most developers disable them for deployed applications.

Install Eclipse

Go to http://www.eclipse.org/downloads/. Choose "Eclipse IDE for Java EE Developers", download, and unzip. Install 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

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 incorrectly fails to copy the welcome pages when it sets up Tomcat, so if you want the friendlier welcome page, go to your-eclipse-workspace\.metadata and search for "ROOT". Copy all of the files from C:\apache-tomcat-6.0.18\webapps\ROOT into the ROOT folder inside your-eclipse-workspace\.metadata\...\ROOT.
Eclipse Running Tomcat
If you fail to copy the ROOT files as mentioned above, http://localhost/ will result in an empty directory listing. It is often mistaken for an error page, but if you look closely you will see that it says "directory listing for /" and is not an error at all. However, if you copy the ROOT files, http://localhost/ will give the nice friendly "Welcome to Tomcat" page.

Import a Sample App and Test

Grab intro.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, you have to restart server after adding a new project. 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/hi HelloServlet via servlet mapping in web.xml.
  • http://localhost/intro/servlet/coreservlets.HelloServlet2 HelloServlet2 via invoker servlet.
  • http://localhost/intro/hi2 HelloServlet2 via servlet mapping in web.xml.
  • http://localhost/intro/servlet/coreservlets.HelloServlet3 HelloServlet3 via invoker servlet.
  • http://localhost/intro/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.
  • WebContent/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 a Java package until you create at least one class first.
Eclipse New Project 3

Testing New Apps in Eclipse

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. To do so, R-click server and choose "Restart".)

More Information

Java

Servlets & JSP

JSF
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA

Struts