Tomcat, Servlets, JSP
SOFTENG 350
April 4, 2005
1 Introduction
The aim of this note is to get you (more) familiar with Servlets and JSP and
the mechanics of using Tomcat. You will be able to use the structures and files
of this example for your assignment.
If any of the directions in this tutorial are misleading, please tell Rick so it
can be updated. These notes assume Tomcat 5.0; the version you use may be
later. The latest version of Tomcat, 5.5, depends on jdk1.5.
We make use of Ant to manage deployment of Servlets, etc, without having
to restart Tomcat. This is easier than copying all the files manually and having
to stop and start Tomcat each time. However, there are some changes that
you’ll need to make to some of the files that are supplied.
After installation, we begin by looking at some Servlet and JSP examples.
2 Working in the Lab
Tomcat and Ant are installed in the Lab. You won’t need your own copies1 .
Run TomCat by selecting
Start/Programs/Software Engineering/Apache Tomcat/Start Tomcat 2 . Point
your browser at http://localhost:8080 and you should see the default Tomcat
home page.
Install the manager webapp: Copy manager.xml from
.../SOFTENG350/resources/JavaEgsForGUILectures/ and place at
H:\sfac apps\tomcat-5.5\conf\Catalina\localhost\. You won’t need to restart
Tomcat. Go to http://localhost:8080/manager in your browser to check that
it’s working.
1 Thanks to Robert Egglestone for the setting up Tomcat to be shared, and providing the
instructions for this Section.
2 This will copy the default Tomcat setup to ”H:\sfac apps\tomcat-5.5” and then start
Tomcat normally.
1
3 Working elsewhere
If you are not doing this tutorial in the Lab, follow the instructions in this
section.
Download and install Tomcat from http://jakarta.apache.org/tomcat/.
Download Ant from http://ant.apache.org/ and unzip it. To make it easy to
run Ant from anywhere, you could add it to the System path:
• Open the System control panel and click on the Advanced tab. Click on
Environment Variables.
• Click on the New button for the User variables at the top of the dialog box.
Enter “ANT HOME ” for the Variable Name and “C:\apache-ant-1.6.1 ”
(or whatever) for the Variable value. Click OK.
• Now select Path in the System variables list and click the Edit button.
Insert “%ANT HOME%\bin;” after a suitable “;” in the Variable value
and click OK.
Copy Tomcat/server/lib/catalina-ant.jar into the Ant/lib folder (only if you’re
not in the Lab).
Run Tomcat by selecting the Start/All Programs/Apache Tomcat/Start Tom-
cat menu.
4 Using Tomcat
Notice the browser links on the left-hand side of the home page, including one
to the Tomcat Documentation and another to Servlet Examples.
Have a quick browse over the documentation, to see what’s there. Then go
back to the home page and click Servlet Examples. Execute the servlet examples
and look at the corresponding source code. Use these little examples to help
understand how simple servlets work.
Go back to the home page and click JSP Examples. Scroll down to the
examples under the heading JSP 1.2 Examples, starting with Numberguess3 .
Execute the JSP examples and look at the corresponding source code, working
down at least as far as the Forward example. Use these little examples to help
understand how simple JSPs work.
5 Running Web Applications
Now it’s time to build a simple web application and deploy it on Tomcat. Once
you’ve done that, you will make changes to it.
Copy the file “TomcatTutorial350.zip” from https://www.se.auckland.ac.nz/
courses/SOFTENG350/resources/JavaEgsForGUILectures/. Unzip it in a work-
ing area, such as on your G: drive.
3 The first set of examples are advanced ones that you may wish to look at later.
2
It consists of several files and folders, many of which are required (and some
of which we’ll change as the tutorial progresses). Here’s a summary of them
now; we’ll discuss how they are used later:
• The bin folder is just there for use by Eclipse. We won’t be using the code
in there because we’ll use Ant to compile and deploy the web application.
• build.xml and build.properties define various tasks that Ant carries out,
such as installing Servlets/JSPs. You will need to change build.xml, as
described below.
• install.bat runs Ant to install the web application for the first time.
• reload.bat runs Ant to reload the web application to change it on the
currently-running Tomcat.
• The build folder is created (and changed) by Ant and holds all of the files
ready for installation or reloading onto Tomcat. Don’t alter these files as
they are recreated from other files by Ant on an install or reload.
• The docs folder holds any documentation files. There is a simple README
in there.
• The src folder holds the Java source files. A single Java class Hello is
defined in the package mypackage.
• The web folder holds the following:
– The images folder holds an image file that is referenced in index.html.
– The WEB-INF folder holds the file web.xml, which defines the map-
ping from a URL to a Servlet. In this case, it maps .../hello to the
servlet mypackage.Hello.
– index.html holds the HTML file that is loaded if the user clicks into
this web application. As we’ll see later, and why, it is accessed
through a browser as “http://localhost:8080/app350/ ”.
– hello.jsp is a JSP file, that is accessed via a link on the HTML page
above.
6 Developing under Eclipse
We’ll now assume that we’ll use Eclipse to develop the code. Create a new
Project under Eclipse:
• Select the menu item File/New/Project.
• Ensure Java is selected and click the Next> button.
• Type in a project name, eg “wui”.
3
• Untick Use default because we want to point Eclipse at existing code.
• Click the Browse... button and select your copy of the folder TomcatTuto-
rial350.
• Click the Next> button at the bottom. The Source folders on build path:
should contain “wui/src” or similar.
• Click on the Libraries tab. Click on the Add External JARS... button
and select “servlet-api.jar ” from “Tomcat 5.0/common/lib”
• Click the Finish button.
• You can select the mypackage package to access the single file, “Hello”, a
Servlet.
We’ll only be using Eclipse to change the Java and xml files in the project.
Ant will be used to build and deploy to Tomcat.
7 Configuring deployment
Now alter the “build.xml ” file in TomcatTutorial350, to specify the location
of the Tomcat code. In the Java perspective that usually shows for a Java
program, double-click on “build.xml ”, to edit it. Scroll down to the single line
(split below):
Change it so that the value string refers to the location of the Tomcat system
you installed. Eg:
8 Deploying Initially
Double-click on install.bat in the TomcatTutorial350 folder. You’ll need to do
this in the file explorer, because it doesn’t work properly if you double-click on
install.bat in Eclipse.
install.bat runs Ant, which creates a fresh copy of the files in the folder build
from other information4 and tells Tomcat to install from that folder as the web
application app350 5 . Ant copies the contents of the web folder into build and
compiles the Java files into there as well.
4 Don’t change the files in the build folder because they will be over-written when you
install or reload the files.
5 As defined in the Ant property app.name in build.xml.
4
9 Running the Web Application
Access the installed web application by pointing your browser at:
http:// localhost:8080/app350/. This shows (a copy of) the page at “Tom-
catTutorial350/web/index.html ”.
Click on the link servlet to see the result of running the servlet mypack-
age.Hello. Go back and click on the JSP page to see the result of running the
JSP.
You can view the Servlet code that’s created by Tomcat when you first run
the JSP page. Look inside the folder:
“Tomcat 5.0\work\Catalina\localhost\app350\org\apache\jsp”.
10 Changing and Reloading
Let’s now try making some changes.:
• Change the file “TomcatTutorial350/web/index.html ”, inserting some text
or changing the HTML tags. Double-click “TomcatTutorial350/reload.bat ”
to reload the web application into Tomcat and reload the corresponding
page in the browser (http://localhost:8080/app350/). You may have to
refresh the browser. Note the change.
• Now change the Servlet Hello in Eclipse. Reload and review the corre-
sponding page in the browser (http://localhost:8080/app350/hello).
• Now change the JSP file “TomcatTutorial350/web/hello.jsp”. Reload and
review that page in the browser (http://localhost:8080/app350/hello.jsp).
• Now change the mapping file, “TomcatTutorial350/web/WEB-INF/web.xml ”.
Alter the url-pattern from “\hello” to ““\hello350”. Reload. Now the link
servlet at http://localhost:8080/app350/ doesn’t work correctly.
• So alter the HTML file “TomcatTutorial350/web/index.html ” so the servlet
link refers to hello35 instead of hello. Reload. Now the link works correctly
again (you may have to refresh the browser page with the link on).
11 Creating another Web application
Copy the whole of “TomcatTutorial350 ” to make up a new folder, such as “My-
WebApp”. Alter the Ant build file “MyWebApp/build.xml ”. Change the value
in the following to “web350 ”:
Install the new application by clicking on “MyWebApp/install.bat”. Now you
can access this other web application through http://localhost:8080/web350/.
5
12 Help and Further Information
See the Tomcat documentation at http://localhost:8080/tomcat-docs/ in your
browser, when running Tomcat.
13 Shut down Tomcat
Shut down Tomcat by double-clicking on Tomcat 5.0/bin/shutdown.bat.
6