J Tomcat A4
J Tomcat A4
20 Oct 2005
The Apache Tomcat application server is no longer the exclusive domain of
advanced Web system developers. In this tutorial, Sing Li shows beginning Web
developers how to leverage their current Java development skills to program
server-side JSPs, servlets, and Web services using Tomcat.
Prerequisites
You need to be familiar with the Java programming language, object-oriented design
Page 1 of 24
developerWorks
ibm.com/developerWorks
principles, and basic TCP/IP networking concepts to get the maximum benefit from
this tutorial. An understanding of the networking APIs in the JDK is ideal, although
not mandatory.
To run the examples in this tutorial, you need:
A working installation of JDK 1.5.0 or later.
A running installation of Tomcat 5.5 or later, available at
http://jakarta.apache.org/tomcat/. This tutorial includes detailed download,
installation, and setup instructions for Tomcat.
To run the Web services example, you also need to install:
Apache Ant 1.5.2 or later, available at http://ant.apache.org/
Apache Axis 1.2.1 or later, available at http://ws.apache.org/axis/. This
tutorial includes detailed installation instructions for Axis.
The recommended system configuration for running the tutorial:
A system supporting JDK 1.5.0 with at least 512MB of memory. The
instructions in the tutorial are based on a system running Microsoft
Windows.
At least 50MB of disk space to install the software and examples.
ibm.com/developerWorks
developerWorks
Page 3 of 24
developerWorks
ibm.com/developerWorks
Page 3 of 11 Document options Print this page PDF - A4 475 KB PDF - Letter 479
KB Get Adobe Reader Sample code Rate this tutorial Help us improve this
content Tomcat installation and setup Downloading Tomcat To download the latest
version of Tomcat, go to the Apache Tomcat home page (see Prerequisites), shown
in Figure 1, and click the Tomcat 5.x link under the Download heading (the area
outlined in red in Figure 1): Figure 1. Apache Tomcat project home page You have a
choice among the latest 5.5.x releases. Choose the binary distribution of the latest
stable (nonbeta and nonalpha) release. For Windows systems, download the EXE
binary for simple installation.
Installing Tomcat
The EXE binary installer does the following:
Unpacks and installs the Tomcat server components.
Lets you specify the TCP port that the server will use when listening for
incoming requests. (A TCP port is a networking endpoint, represented by
ibm.com/developerWorks
developerWorks
Description
License agreement
Page 5 of 24
developerWorks
ibm.com/developerWorks
Choose components
Configuration
Completing the Apache Tomcat Setup Wizard This is the final step of the installation. Select the
Run Apache Tomcat checkbox. This starts the
system service immediately after installation.
Note that on some versions of Windows with a firewall, you might need to give
Tomcat explicit permission to listen to the TCP port for requests.
After installation, the Tomcat server will be running, with the Apache service-monitor
icon appearing in the lower right-hand corner of your Windows Taskbar (the long bar
on the bottom of the screen), as shown in Figure 3:
Figure 3. Service monitor showing Tomcat running
In Figure 3, the green arrow on the monitor icon indicates that the Tomcat service is
running.
ibm.com/developerWorks
developerWorks
The Tomcat server is listening at port 8080. (You configured this during the
installation.) Figure 4 shows the welcome screen that Tomcat displays:
Figure 4. Tomcat's welcome screen
By running the Tomcat server on the same machine as your browser, you are
simulating a networked environment. Figure 5 shows this loop-back network
configuration:
Figure 5. Loop-back configuration for single-machine server-side development
Page 7 of 24
developerWorks
ibm.com/developerWorks
In Figure 5, both the client (browser) and the server (Tomcat) are running on the
same machine. The TCP connection between the client and the server is running in
a loop-back mode. This is a common practice in Web development, enabling you to
perform server-side development using a single machine. In actual production, you
can change the host name of the URL from localhost to the IP address of your
networked production Tomcat server (shown within dashed lines in Figure 5).
ibm.com/developerWorks
developerWorks
Standard actions
Expression Language (EL)
Custom tag libraries
JavaBeans
JSP has built-in capability to access JavaBeans. In production applications,
JavaBeans are typically used to carry data values between the application logic
(implemented using servlets and other components) and JSP. The JSP code's
typical responsibility is to display the value contained in the JavaBean.
One of the more frequently used tag libraries for JSP is the JSP Standard Tag
Library (JSTL). JSTL, defined in JSR 52 (see Resources), contains a large library of
tags that can be used in conjunction with the EL in JSP. The latest version of JSTL
(as of October 2005) is 1.1.
Unlike Java programs, JSP programs need not be precompiled. Tomcat compiles a
JSP the first time it is executed and keeps a copy of the compiled binary for
subsequent execution. This allows for rapid development and testing cycles.
With early versions of JSP (before 2.0), it was difficult to write general application
logic without resorting to embedded Java coding. In fact, JSP versions prior to 2.0
allowed and encouraged the use of mixed Java/JSP coding. This practice generally
creates code that is messy and difficult to maintain.
Beginning with JSP 2.0, with its support for EL and JSTL, embedding Java code in
JSP programs is no longer necessary. It is recommended that all new JSP
developers not intermix embedded Java code with JSP. This approach is often
called scriptless JSP.
There's a lot more you can learn about JSP programming after you finish this tutorial
(see Resources for more information on JSP). The rest of this section shows you
how to create and run JSP applications using Tomcat, so you can start trying out
your own JSP programming right away.
Page 9 of 24
developerWorks
ibm.com/developerWorks
Create your JSP application. If you are working with only one page, call it
index.jsp, as you've done for the example program.
ibm.com/developerWorks
developerWorks
2.
3.
4.
Bundle all the code, using the JAR tool from the JDK, into a Web
application archive (WAR) file for deployment.
5.
Use the Tomcat Web Application Manager to deploy and run the WAR
file.
The WAR file is a standard Java Enterprise Edition (Java EE) deployment unit. It is a
JAR file in a very specific format, with a .war filename extension. In this WAR file,
you must have a deployment descriptor file called web.xml, which contains
instructions telling the server how to deploy the content of the WAR.
n the case of the example program, the web.xml file (see Listing 2) is trivial, because
the application contains only one JSP page:
Listing 2. The web.xml deployment descriptor
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>
developerWorks Beginning Tomcat Tutorial
</description>
<display-name>IBM developerWorks Beginning Tomcat Tutorial
</display-name>
</web-app>
The web.xml file in Listing 2 simply provides a description and display name to
Tomcat, which will be used later by the Tomcat Application Manager.
To create the WAR file, run the makewar.bat batch file in the code distribution (see
Download). This batch file simply calls the JAR utility to create a JAR file. Listing 3
shows the contents of makewar.bat:
Listing 3. makewar.bat
jar cvf step1.war .
In you're on a Linux system, you can enter jar cvf step1.war . at the console
to create the WAR file manually.
Page 11 of 24
developerWorks
ibm.com/developerWorks
Manager
To run the application on Tomcat, you need to deploy the WAR file first. Use the
Tomcat Web Application Manager (Manager for short) utility for this. You can access
Manager by pointing your browser to http://localhost:8080/manager/html.
Tomcat will ask you for a username and password. Enter the administrator user
name and password that you supplied during setup. Once you have logged in to the
server, you will see the Manager's display. It shows all the applications that are
currently loaded and running on the Tomcat server, as shown in Figure 6:
Figure 6. The Tomcat Web Application Manager utility
To deploy step1.war, scroll down to the bottom of the Manager page. Click the
Browse button next to the WAR file to deploy section. Use the browser to select
the step1.war file, then click the Deploy button. This action sends the WAR file to the
Tomcat server and starts it.
You should now see the step1.war application running on Manager's list of running
ibm.com/developerWorks
developerWorks
applications. Note also that this list uses the display name you set in web.xml to
identify the application.
Finally, you can see the JSP application running by pointing a browser to
http://localhost:8080/step1/.
By default, Tomcat uses the name of the WAR file to provide a context for the
application. The context is specified as part of the address you used to access the
application. In this case, the context is step1. Tomcat looks for a file called index.jsp
in the application's root directory to execute if it exists. Figure 7 shows the running
JSP application:
Figure 7. A running JSP application on Tomcat
If you make any modifications to the JSP code (as you learn JSP), you can perform
the following steps to run the new code:
1.
2.
3.
Page 13 of 24
developerWorks
ibm.com/developerWorks
2.
3.
ibm.com/developerWorks
4.
developerWorks
package com.ibm.dw.tutorial.tomcat;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
java.io.IOException;
2.
3.
4.
Page 15 of 24
developerWorks
ibm.com/developerWorks
You can see that the same techniques used Listing 1 for the step1 example in the
preceding section (Your first JSP application on Tomcat) are used here:
The JSTL date formatting tag formats a java.util.Date JavaBean
instance.
The <c:forEach> JSTL loop tag iterates through the List attribute of
specials (attached to the request object by SpecialServlet).
EL expressions display the value of the specials.
ibm.com/developerWorks
developerWorks
about the servlet and how to map it to incoming requests. Listing 6 shows the
web.xml file for this example:
Listing 6. web.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>developerWorks Beginning Tomcat Tutorial
</description>
<display-name>
IBM developerWorks Beginning Tomcat Tutorial Step 2
</display-name>
<servlet>
<servlet-name>Specials</servlet-name>
<servlet-class>
com.ibm.dw.tutorial.tomcat.SpecialsServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Specials</servlet-name>
<url-pattern>/showspecials.cgi</url-pattern>
</servlet-mapping>
</web-app>
This deployment descriptor includes a <servlet> element that tells Tomcat about
the servlet's implementation class. The <servlet-mapping> element tells the
server that requests destined for showspecials.cgi should be passed to this servlet.
You must compile the servlet before you can create the WAR file. Because servlets
are Java code, they must be compiled before they can be deployed. You can use
the compile.bat batch file in the code distribution (see Download) for this purpose.
However, you need to modify it to point to your own Tomcat directory, because
servlet-api.jar from Tomcat's common/lib directory contains some interfaces and
helper classes that are required to compile the servlet.
After you've compiled the application successfully, you can make the WAR file by
executing makewar.bat. The WAR file is called step2.war this time. Once the WAR
file is created, you can deploy it using the Tomcat Manager.
You can access the Web application by pointing a browser to
http://localhost:8080/step2/showspecials.cgi.
Tomcat now maps the URL path of showspecials.cgi to the servlet code. This was
specified in web.xml by a <servlet-mapping> element. Figure 8 shows the output
of the application, displaying the menu specials:
Figure 8. SpecialsServlet running on Tomcat
Page 17 of 24
developerWorks
ibm.com/developerWorks
If you make changes to the servlet code, you can run the new code with these steps:
1.
2.
3.
4.
ibm.com/developerWorks
developerWorks
servlets.
Web services are becoming increasingly popular because they can be effectively
used for business-to-business or business-to-consumer interfaces. They let requests
be sent, and responses received, through the Internet. Any user who can access
your Web site can also access your Web service(s). For example, both eBay and
Amazon.com offer Web services that their partners and users can consume.
Web services depend on passing XML-based messages between the consumer and
the service. The messages are packaged and sent according to the Simple Object
Access Protocol (SOAP).
Apache Axis is a Web services development kit that can be used as an add-on to
Tomcat. The next section shows you how to create a simple Web service, using
Apache Axis, and deploy it on your Tomcat server. See Resources for articles and
tutorials that can help you learn more about Web services programming.
Page 19 of 24
developerWorks
ibm.com/developerWorks
The code for the Web service is contained in the ShowSpecials.jws file, shown in
Listing 7:
Listing 7. ShowSpecials.jws
public class ShowSpecials {
public String [] getMenuItems() {
return new String []{
"Coq au Vin", "Pad Thai",
"Lobster Thermador", "Baked Alaska" };
}
public int [] getPrices() {
return new int [] { 15, 10, 10, 8 };
}
}
The code in Listing 7 has two public methods. The getMenuItems() method
returns the menu items that are on special, and the getPrices() method retrieves
their price.
Axis supports an instant-deployment mode using .jws (Java Web services) files. In
this mode, all you need to do is place a Java source file with a .jws extension in the
axis directory. Any public methods of this class will be exposed through the Web
service. This example uses the instant-deployment mode. The getMenuItems()
and getPrices() methods are automatically exposed by Axis as Web service
methods when it parses and compiles the .jws file. The parsing and compilation
occur only when you first access the Web service.
ibm.com/developerWorks
developerWorks
To start the client, consuming the Web service, run Ant with the following command:
ant run
Typical output from a successful run of the Web service client, showing the specials
menu items and price, looks like this:
Buildfile: build.xml
run:
[java] Specials today:
[java]
Coq au Vin
[java]
Pad Thai
[java]
Lobster Thermador
[java]
Baked Alaska
$15
$10
$10
$8
BUILD SUCCESSFUL
Total time: 7 seconds
Section 7. Summary
The Tomcat server is a great platform for learning JSPs, servlets, and Web services
programming. In this tutorial, you have learned how to:
Page 21 of 24
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
Downloads
Description
Name
Size
j-tomcatsource.zip12KB
Download method
HTTP
Page 23 of 24
developerWorks
ibm.com/developerWorks
Resources
Learn
Tomcat Web site: Tomcat documentation, how-tos, and mailing lists.
Java Community Process: You can find the Java Specification Requests for
JavaServer Pages, Java Servlets, and the JSP Standard Tag Library here.
"Introduction to JavaServer Pages technology" (Noel J. Bergman,
developerWorks, August 2001): Learn JSP programming with this tutorial.
"Introduction to Java Servlet technology" (Roy Miller, developerWorks,
December 2004): This tutorial uses Eclipse's Tomcat plug-in to make servlet
development on Tomcat even easier.
New to SOA and Web services: Get a handle on the basics of Web services
technology.
SOA and Web services zone: Find articles and tutorials about every aspect of
Web services programming.
Java technology zone: Find more resources for Java developers.
Get products and technologies
Tomcat downloads page: Find the latest Tomcat download.
Apache Axis: Download the Axis Web services engine.
Apache Ant: You need to install Ant, a versatile build tool for Java projects, to
build the Web service client in this tutorial.