Tomcat Java Server

Tomcat is Apache's Java Server.  It is designed to run Both as a special purpose Servlet engine to which an Apache HTTP server passes Servlet requests or as a standalone server that processes both HTTP and servlet requests.  But, it is not as efficient as Apache for HTTP tasks.  In is the current Java Server of choice for this course, and I recommend that you install a version of it on the system you use for development.  You should debug your servlets there and then move them to your course directory only when they are well-behaved.

The goal of this lesson is to provide a checklist of some of the main tasks required to install and configure Tomcat for personal servlet development use.


1. Download Tomcat

You can get a copy of the version of Tomcat (3.2.1) that is currently being used for this course (wwwj.cs.unc.edu:8888) from two local sources: 

  1. The course tools directory:  /afs/cs.unc.edu/project/courses/comp118-s02/tools/Tomcat/jakarta-tomcat-4.0.4/

    which you can view from your web browser at:
    http://www.cs.unc.edu/Courses/comp118/tools/Tomcat/jakarta-tomcat-4.0.4/.
  2. You can also get a copy from the public directory on my personal PC server:
    jbssrv-cs.cs.unc.edu.  Of course, you will need to access that version through Network Neighborhood.

2. Install Tomcat

"Installing" earlier versions of Tomcat involved simply copying the directory to your machine and putting it in a convenient location.  You didn't go through a typical "install" procedure involving an install shield, etc., as required for most software.  The current version (4.0.4) includes a standard installation shield.  Execute it and follow the directions.  There is also an option for installing it so that it runs automatically whenever your system is booted; I don't recommend that configuration for servlet development, but if you are interested, you can explore the documentation to see how you do that.

I suggest that you set up a directory where you are going to do your Java development work for this course and create a directory called tomcat there.  In discussing paths, etc., below, I'll assume that you have such a directory.

3. Run Tomcat

To run Tomcat, you have two options.  You can start and stop it from the start button or you can open a Command Prompt window and cd to the tomcat root directory (e.g., ...\tomcat).  Then, type the command:  bin\startup.  The system will open a second command window and report various parameters and conditions pertaining to Tomcat.  You can minimize/iconize both windows, but don't close them.

To stop Tomcat, follow the same procedures, but type the command: bin\shutdown.  the system will give you some information about shutting down Tomcat and will close the second window.

You can run Tomcat as it comes "out of the box," but you are likely to want to set several parameters to tailor it to your environment.  The next item discusses several.

3. Set Parameters

You can set a bewildering range of configuration parameters.  Several that I suggest changing are discussed, below.

The parameters you are likely to want to change are contained in the conf directory, specifically the server.xml file.  Before you change anything in the server.xml file, make a copy of it.

Don't be intimidated by the XML if you are not familiar with it.  You are likely to have to change only a value in a couple of places or copy and past a small block of statements in which you will then change a value or two.  Some of the parameters you may wish to change are the following:

port:  it is set, by default, to 8080; I suggest changing it to 8888 to make it compatible with the wwwj:8888 server we are using for this course.  Search for port in the file and change the value there.

document root:  by default, Tomcat uses the webapps/ROOT directory as the root of its document tree.  You can change it -- for example, to your local Java development directory; a public_html directory, if you have one; or any other directory on your machine.

To do so, locate near the bottom of the server.xml file a block of text something like the following:

        <Context path="/examples" 
                 docBase="webapps/examples" 
                 crossContext="false"
                 debug="0" 
                 reloadable="true" > 
        </Context>
To change, say, the document root, set path="" and docBase="E:/MyDocuments/public_html" or to wherever you want the server to go for your default directory.

5. Locate the Servlets Directory

By default, Tomcat looks for servlets in the webapps/WEB-INF/classes directory.  You may place your servlets class files there and then access them with your local browser as: http://localhost:8888/servlet/my_servlet

If you wish to access them from some other directory, first, create a new context to that directory, as described in step 4, above, and then add a WEB-INF/classes directory and subdirectory to it.  You may then access it as:  http://localhost:8888/new_directory/servlet/my_servlet.  That is what we have done for the wwwj:8888 server so that you may access your course servlets from a WEB-INF/classes directory and subdirectory in your personal class directory under members.  For example, I have a servlet called jbsGetPostEcho that can be accessed from the following url: http://wwwj.cs.unc.edu:8888/jbs/servlet/jbsGetPostEcho. You can do the same.