ONJava.com    
 Published on ONJava.com (http://www.onjava.com/)
 See this if you're having trouble printing code examples


Using Tomcat

Installing and Configuring Tomcat

03/29/2001

Also in Using Tomcat:

Configuring Tomcat with IIS Web Server

Configuring Tomcat and Apache With JK 1.2

Demystifying Tomcat 4's server.xml File

Embedding Tomcat Into Java Applications

Using SOAP with Tomcat

This article, in which we examine issues specific to Tomcat, is the second in our series on the Jakarta-Tomcat server. In this article we will discuss

Requirements for Installing and Configuring Tomcat

Before performing the tasks outlined by this article you will need to download the items listed in the Table 1.

Table 1. Requirements

Name Location
Tomcat 4.0 beta 1 http://jakarta.apache.org/
JDK 1.3 Standard Edition

For this article we will be using the latest versions of the tools listed above.

Installing and Configuring Tomcat

In this article we will be installing Tomcat as a stand-alone server. This means that Tomcat will service all requests, including static content, JSPs, and servlets.

To configure Tomcat as a stand-alone server you will need to download the Tomcat 4.0 beta 1 and the JDK 1.3 Standard Edition from the locations listed above. You should choose the appropriate downloads based on your OS. We will be covering the steps involved in installing to both NT/2000 and Linux.

Installing to Windows NT/2000
The first installation we will be performing is to Windows NT/2000. The first thing you need to do is install the JDK, following its installation instructions. For this article I am installing the JDK to drive D:, therefore my JAVA_HOME directory is D:\jdk1.3.

Now you need to extract the Tomcat server. Again, I am installing to drive D:, which will make my TOMCAT_HOME directory D:\jakarta-tomcat-4.0-b1.

After you have extracted Tomcat, the next step is putting your JDK into Tomcat's CLASSPATH and setting the TOMCAT_HOME environment variable. To do this under NT/2000, you must

  1. Open the NT/2000 Control Panel. You should see an image similar to Figure 1.

    Screen shot.
    Figure 1. NT/2000 Control Panel

  2. Start the NT/2000 System Application and select the Advanced tab. You should see a screen similar to Figure 2.

    Screen shot.
    Figure 2. NT/2000 System Application

  3. Select the Environment Variables button. You will see a screen similar to Figure 3.

    Screen shot.
    Figure 3. Environment Variables Dialog

  4. Select the New button on the System Variables section of the Environment Variables dialog. Add a JAVA_HOME variable and set its value to the location of your JDK installation. Figure 4 shows the settings associated with my installation.

    Screen shot.
    Figure 4. JAVA_HOME Environment Settings

  5. Repeat Step 4 using TOMCAT_HOME for the variable name and the location of your Tomcat installation as the value. For my installation I am setting the value to D:\jakarta-tomcat-4.0-b1.

That's all there is to it. You should skip the following section "Installing to Linux" and move on to "Testing You Tomcat Installation."

Installing to Linux
The installation is much simpler on a Linux than a Windows machine. The first thing you need to do is install the JDK. For our purposes, we will assume that the JDK will be installed to /bob/java.

After you have the JDK installed, you need to set the JAVA_HOME environment variable. To do this under Linux, find your shell in Table 2 and type the matching command. You will need to replace /bob/java with the root location of your JDK installation.

Table 2. Java Environment Settings

Shell JAVA_HOME
bash
JAVA_HOME=/bob/java
tcsh
setenv JAVA_HOME /bob/java

You should add the location of the Java interpreter to your PATH environment variable.

You now need to extract the Tomcat server to a directory of your choosing. This directory will become the TOMCAT_HOME directory. For this installation, we will assume that Tomcat will be installed to /bob/jakarta-tomcat-4.0-b1.

The last step is to set the TOMCAT_HOME environment variable. To do this under Linux, find your shell in Table 3 and type the matching command. You will need to replace /bob with the name of the directory located directly above your Tomcat installation.

Table 3. Tomcat Environment Settings

Shell TOMCAT_HOME
bash
TOMCAT_HOME=/bob/jakarta-tomcat-4.0-b1
export TOMCAT_HOME
tcsh
setenv TOMCAT_HOME /bob/jakarta-tomcat-4.0-b1

Testing Your Tomcat Installation

To test the Tomcat installation, first start the Tomcat server. Table 4 contains the startup and shutdown commands for each OS.

Table 4. Tomcat Startup/Shutdown Commands

OS Startup Shutdown
Windows NT/2000 TOMCAT_HOME\bin\startup.bat TOMCAT_HOME\bin\shutdown.bat
Linux TOMCAT_HOME/bin/startup.sh TOMCAT_HOME/bin/shutdown.sh

Once Tomcat has started, point your browser at

http://localhost:8080/

You should see a page similar to Figure 5.

Screen shot.
Figure 5. The Tomcat Default Page

If you would like to have all requests serviced on the default HTTP port of 80, instead of port 8080, you will need to make the following change to the TOMCAT_HOME/conf/server.xml file and restart Tomcat.

Change

<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="8080" minProcessors="5" maxProcessors="75"
acceptCount="10" debug="0"/>

to

<!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="80" minProcessors="5" maxProcessors="75"
acceptCount="10" debug="0"/>

Now point your browser at

http://localhost/

and you'll see results similar to those in Figure 5.

The next step is to verify the installation of your JDK, which is done by executing one of the JSP examples provided with the Tomcat server. At the page shown in Figure 5, choose JSP Examples. You should see a page similar to Figure 6.

Screen shot.
Figure 6. The JSP Examples Page

Now choose the JSP example Date and select the Execute link. If everything was installed properly you should see a page similar to Figure 7 (of course with a different date).

Screen shot.
Figure 7 The JSP Date Page

If you do not see the previous page, then you need to make sure that the location of your JAVA_HOME environment variable matches the location of your JDK installation.

Deploying Web Applications to Tomcat

Once Tomcat is installed and running, let's look at the steps necessary to deploy a web application. To deploy a web app, we need to examine the directory structure of Tomcat. Table 5 describes the directories that make up a Tomcat installation. It is assumed that the value of TOMCAT_HOME precedes each of these directories.

And because we are using a beta release of Tomcat, these directories could change without notice.

Table 5. The Tomcat Directory Structure

/bin

This directory contains the startup and shutdown scripts for both Windows and Linux.

/conf

This directory contains the main configuration files for Tomcat. The two most important are the server.xml and the global web.xml.

/server

This directory contains the Tomcat Java Archive files.

/lib

This directory contains Java Archive files that Tomcat is dependent upon.

/logs

This directory contains Tomcat's log files.

/src

This directory contains the source code used by the Tomcat server. Once Tomcat is released, it will probably contain interfaces and abstract classes only.

/webapps

All web applications are deployed in this directory; it contains the WAR file.

/work

This is the directory in which Tomcat will place all servlets that are generated from JSPs. If you want to see exactly how a particular JSP is interpreted, look in this directory.

We will examine most of these directories in future articles. For the remainder of this article we're interested in the /webapps directory, which is where all of our WAR files will be deployed.

In our last article we described the contents of a web application and how they are packaged. Once you have a WAR file, containing your web application, deploying web applications to Tomcat is a simple two-step process.

Steps Involved in Deploying a Web Application to Tomcat

  1. Copy your WAR file to the TOMCAT_HOME/webapps directory.

  2. Add a new Context entry to the TOMCAT_HOME/conf/server.xml file, setting the values for the path and docBase to the name of your web application.

    <Context path="/onjava" docBase="onjava" debug="0" reloadable="true" />

Restart Tomcat after completing these steps. Your application should now be running.

The previously described application can be accessed by pointing your browser at

http://localhost/onjava/

If you look at the TOMCAT_HOME/webapps directory, you will see a new directory matching the name of your WAR file. This is where your working web application now exists. When Tomcat starts it will extract all WAR files that have been recently placed into the TOMCAT_HOME/webapps directory.

In the next article we will learn how to add Servlets, JSPs, and custom tag libraries to a web application. We will also discuss the relationship between a web application and its ServletContext.

James Goodwill is the co-Founder of Virtuas Solutions, LLC, a Colorado-based software consultancy.


Read more Using Tomcat columns.

Return to ONJava.com.

Copyright © 2009 O'Reilly Media, Inc.