Deploying Servlets and JSPs on Tomcat - (Draft)


Abdul Habra, 10.16.2001

Table Of Contents

1. TOMCAT INSTALLATION
    1.1. LOCATION
        1.1.1. JDK Location
        1.1.2. Tomcat Location
    1.2. START AND STOP
    1.3. DIRECTORIES
2. DEPLOYING APPLICATIONS OF SERVLETS CLASSES
    2.1. CREATING CLASSES
    2.2. DEPLOYING IN DEFAULT DIRECTORY
        2.2.1. Running
        2.2.2. Note
    2.3. DEPLOYING IN ANY DIRECTORY
        2.3.1. Preparing Directories
        2.3.2. Modifying server.xml
        2.3.3. Modifying web.xml
        2.3.4. Running
3. REFERENCES


This document describes how to deploy Servlets and JSPs to Tomcat 3.2.3 on an MS-Windows system.

1. Tomcat Installation

1.1. Location

1.1.1. JDK Location
Let the JDK be installed in the directory C:\Java\jdk1.3.0\

Set the following environment variable (using System Properties/Environment)

JAVA_HOME=C:\Java\jdk1.3.0\


1.1.2. Tomcat Location

Let Tomcat be installed in the directory: C:\Java\Jakarta\Tomcat\jakarta-tomcat-3.2.3\

Let's call this directory TH (for Tomcat Home)

Set the following environment variable (using System Properties/Environment)

TOMCAT_HOME=C:\Java\Jakarta\Tomcat\jakarta-tomcat-3.2.3\

1.2. Start and Stop

To start Tomcat, go to TH/bin and at the command line run startup.

To stop Tomcat, in the same command window, run shutdown.

To verify that Tomcat is running, in a browser window go to http://localhost:8080/index.html which will show you a page with Tomcat info and examples.

1.3. Directories

TH has a directory named webapps, this is the default directory for your web applications. Put your .war files in the webapps directory.

2. Deploying Applications Of Servlets Classes

This section will explain how to deploy an application that consists of Servlet classes.

2.1. Creating Classes

Assume that we have:
  1. An application in a package named pack1
  2. The application consists of one Java class C1.java
  3. C1 is a Servlet, i.e. it extends HttpServlet class.
  4. C1 is in the pack1 package.
  5. Compile C1.java to get C1.class

2.2. Deploying In Default Directory

  1. In TH\webapps\ create a directory named ap1
  2. In ap1 create a directory named WEB-INF
  3. In WEB-INF create a directory named classes
  4. In classes create a directory named pack1
  5. By now we have the following path: TH\webapps\ap1\WEB-INF\classes\pack1\
  6. Copy C1.class into pack1 directory.
2.2.1. Running
To test the Servlet, go to this URL in the browser http://localhost:8080/ap1/servlet/pack1.C1

This will run your Servlet. Notice that the use of the word servlet in the URL's path does not correspond to an actual directory.

2.2.2. Note
If the Servlet class did not have a package we would have copied it in TH\webapps\ap1\WEB-INF\classes\ (no pack1 directory), and would run it with the URL http://localhost:8080/ap1/servlet/C1

2.3. Deploying In Any Directory

2.3.1. Preparing Directories
  1. Assume that we want to install the application in a directory named C:\jap1\
  2. In C:\jap1\ create a directory named WEB-INF
  3. In WEB-INF create a directory named classes
  4. In classes create a directory named pack1
  5. By now we have the following path: C:\jap1\WEB-INF\classes\pack1\
  6. Copy C1.class into pack1 directory.

2.3.2. Modifying server.xml
  1. Using a text editor, open the file TH\conf\server.xml
  2. Find the </ContextManager> tag. (Note: this is an end tag)
  3. Directly in the line before it, insert the following:
    <Context path="/jap1"
        docBase="c:/jap1"
        crossContext="false"
        debug="0"
        reloadable="true" >
    </Context>
  4. Save and close the file.
This tells the server to map requests that start with /jap1 (after the server name and port number) to the directory c:/jap1

2.3.3. Modifying web.xml
  1. Using a text editor, create the file: C:\jap1\WEB-INF\web.xml
  2. Copy the following text into the file:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
        "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

    <web-app>
      <servlet>
        <servlet-name> localAlias </servlet-name>
        <servlet-class> pack1.C1 </servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name> localAlias </servlet-name>
        <url-pattern> /MyServet </url-pattern>
        </servlet-mapping>
    </web-app>
  3. Save and close the file.
This tells the server to map requests for /MyServlet to pack1.C1 Servlet class.

Note that the value of the <servlet-name> element, which is localAlias, is arbitrary and is not visible outside the web.xml file.

2.3.4. Running
To test the Servlet, go to this URL in the browser: http://localhost:8080/jap1/MyServlet

This will run your Servlet.

3. References

In addition to Tomcat's documentation, the following links can be helpful:
  1. Sun's Servlets Tutorial: By Cynthia Bloch and Stephanie Bodoff.
  2. Devx.com: A Quick Guide to Installing Tomcat on Windows: By Piroz Mohseni.

Comments