Eclipse & Tomcat's "Example" Webapp
(Free Web Tutorial)
by Michael Thomas
Tomcat Home Page
Setup Eclipse projects for the Tomcat Example webapps that get
installed with Tomcat so you can edit & compile changes to existing examples.
On the Eclipse Home page, you can download
the whole Eclipse site (all content, tutorials & examples) !!!
This tutorial covers: Eclipse 3.1.1 (as of 05/14/08), Apache Tomcat
6.0.16 (as of 05/25/08)
Prerequisites
- Have Eclipse installed.
- Have Tomcat installed (with Struts if you want to complete that section).
- Know how to Start/Stop Tomcat and/or Reload a WebApp (we have a
Tomcat Tutorial on that).
- This tutorials has been tested in the following environment:
Date: 08/12/2010
OS: Win 7
JDK: jdk1.6.0_21
(Older Tutorial -
Date: 05/14/08, OS: Win XPro, JDK: jdk1.6.0_03)
Objectives
- Create a new Eclipse project tied to existing webapps running on Tomcat
that were installed with Tomcat.
- With a copy of the Tomcat examples webapp you can now make changes and
experiment.
General Info
- Reload Web App or Stop/Start Tomcat Service
- FYI - Remember if you make changes to the web.xml or
.java source files you will need to "Reload" the Webapp or Start/Stop the "Apache Tomcat" service.
(Note: There is a free Tomcat Tutorial which covers those steps.)
- To Stop/Start the Tomcat service:
Control Panel, Administrative Tools, Services, then right click on "Apache
Tomcat" and choose Stop, then Start.
- To reload the webapp:
http://localhost:8080
, click "Tomcat Manager", find the webapp and choose "Reload"
examples - Tomcat's example webapp.
- Verify you have Tomcat's example webapp.
- Using Win Explorer find the Tomcat Webapp directory.
Possible directory:
ex: C:\Tomcat\Tomcat6.0\webapps\webapps
- examples - if this webapp exits (the directory) then you can
continue this tutorial. This directory was an optional install
when you installed Tomcat.
(If you don't have the files you could try to rerun the install and only
choose the "Examples".)
- http://localhost:8080/examples
- make sure Tomcat is running then verify this works!
- Create the Webapp
- Stop Tomcat
- Let's copy the existing web app so you can make changes without changing
the original web app.
- Go to the Tomcat webapps directory:
Ex: C:\Tomcat\Tomcat6.0\webapps\webapps
- Make a copy of the directory "examples" and name it:
myeclipse-tomcat-examples
- Verify the new webapp works
- Eclipse - Create a Java Project -
- Launch Eclipse
- File, New, Project, Java Project, Next
- Project Name:
myeclipse-tomcat-examples
- UnCheck - Use default location.
- Click "Browse" and locate the directory in the Tomcat WebApp
directory.
Click "OK"
Ex Directory:
C:\Tomcat\Tomcat6.0\webapps\myeclipse-tomcat-examples
- Click "Next"
- Check the box - Allow output folders
for source folders.
Note: This webapp has the .java & .class files in the same
directory (which is not advised for real web apps).
- On the "Java Settings" screen change the "Default output folder":
myeclipse-tomcat-examples/WEB-INF/classes
(Note: This webapp has a "plugin" directory that has .java
files that do not match the scheme of the rest of the .java & .class
files. Ex directories: WEB-INF\jsp\applet & jsp\plugin\applet\examples)
- Click "Finish"
- WARNING READ THIS: If you see the prompt "The output folder has changed ...." click
"No".
If you click yes you will delete a needed directory.
- Eclipse - Build Path - add the Tomcat lib jars files.
- Right click on the project.
- Click "Build Path", "Configure Build Path", then
click the tab "Libraries"
- Check to make sure that the "JRE System
Library" is listed.
If not, click "Add Library", "JRE System
Library", "Next", "Finish".
Notes:
Under the tab "Order and Export" this lib file should be directly under
the last source directory listed.
You can't compile .java files without this library!!!
- Click "Add External JARs"
- Browse to the Tomcat common library directory.
Ex: C:\Tomcat\Tomcat6.0\common\lib
- Select all (Ctrl-A) of the JAR files and click "Open".
- Click "OK" to leave the "Java Build Path" screen.
- You're finished.
- Note about errors:
- Clock2.java is defined in 2 different places:
1) /myeclipse-tomcat-examples/jsp/plugin/applet/Clock2.java
2) /myeclipse-tomcat-examples/WEB-INF/jsp/applet/Clock2.java
- /myeclipse-tomcat-examples/WEB-INF/jsp - need to find out why one day.