Java Web Programming with Eclipse contents
Last modified January 14, 2011 04:55 am

back next

The Apache Tomcat Web Container




Tomcat is a J2EE Web Container, which means it is a Web server and a platform to run Java Servlets and JSP. Tomcat is an open source project managed by the Apache Software Foundation.

This chapter guides you through the installation of Tomcat, explains Tomcat class loading issues, and shows how to perform a deep restart of Tomcat.

Install Tomcat

Go to the Apache Tomcat web site, and navigate to the download page for the most recent release of Apache Tomcat version 7. At the time of this writing, this was 7.0.6. Download a binary release of the Core distribution for your system. Under Windows, you can use the Windows Service Installer or zip file. Under Linux or Mac, you should download the file ending with the .tar.gz extension.

The following command illustrates how to expand a .tar.gz file under Linux.

tar -zxvf file-name.tar.gz

If you downloaded the Windows service installer, then run it to install Tomcat. Otherwise, follow the instructions in the distribution or on the tomcat web site to install tomcat on your system.

In this book the folder in which tomcat is installed is referred to as ${TOMCAT_HOME}.


After installation is complete, test that everything is OK. Under Windows run Configure Tomcat, which is found through the Start menu. After starting Tomcat, go to the the following url in a browser to see if tomcat is running.


You should see something like the following.


Tomcat doesn't start under Vista

If you installed Tomcat as a service, you need to run Tomcat as administrator. Right click on the Tomcat Configuration in the start menu and select Run as administrator.

Tomcat doesn't start under Windows

Open up the tomcat log file jakarta_service_* and see if it resembles the following.

[2009-01-02 17:08:20] [info] Procrun ( started
[2009-01-02 17:08:20] [info] Running Service...
[2009-01-02 17:08:20] [info] Starting service...
[2009-01-02 17:08:20] [174  javajni.c] [error] The specified module could not be found.
[2009-01-02 17:08:20] [994  prunsrv.c] [error] Failed creating java C:\Program Files\Java\jre6\bin\client\jvm.dll
[2009-01-02 17:08:20] [1269 prunsrv.c] [error] ServiceStart returned 1
[2009-01-02 17:08:20] [info] Run service finished.
[2009-01-02 17:08:20] [info] Procrun finished.

If this is your case, then the problem is that msvcr71.dll is not being loaded. The solution is to add the folder that contains this dll to your Path. For example, if the Java binaries are in C:\Program Files\Java\jre6\bin, then append the following onto your system Path. (Path is an environmental variable, which you can modify through Control Panel.) Modify this example path to point to the location of the Java binaries in your system.

;C:\Program Files\Java\jre6\bin

You may need to restart your system (or logout/login) in order to make changes to the Path variable effective.

If you are using more than one version of Java, then adding Java to the Path variable is not advised because other applications may not run correctly. One possible solution is to start tomcat with something like the following.

cd C:\Users\User\apache-tomcat-6.0.18\bin
set JRE_HOME=C:\Program Files (x86)\Java\jre6

Configure Firewall

If you are running security software on your computer, such as a firewall program, you need to configure it so that the Tomcat server has sufficient privileges. The privileges needed by Tomcat include the following:

  1. Tomcat should be able to listen on communication ports. (Also called binding to ports.)
  2. Tomcat should be able to make connections to other computers on the internet.

If you want other computers to be able to connect to Tomcat in order to access your web applications, you will need to open port 8080 to the outside world. However, if your network does not allow for you to accept incoming TCP connections, as is the case in some university wireless networks, others will not be able to connect to your Tomcat server from other computers.

Manager Application

From the Tomcat start page, follow the Tomcat Manager link. Log in as admin with the password you provided when installing tomcat. This application lets you view the Web applications that are deployed in the server and allows you to modify their state. The following figure shows the Tomcat Web Application Manager.

Notice there are currently 4 Web applications deployed to the server. In the column labeled Path, you can see the Context Path for each application. For service requests (from Web browsers, or other clients) to reach a particular Web application, the client must include the context path within the URL it uses to reach the server. Web service URLs are comprised of four pieces of information: the protocol (which is http), the fully qualified DNS hostname (which is localhost for the local machine), the port number on which the web service is listening (which is 8080 in the default configuration of Tomcat), and a pathname to a particular service (which is /manager/* for various services available through the manager application). For example, to reach the home page of the manager application currently running in your machine, you would use the URL http://localhost:8080/manager.

In this book, you will use the manager application to deploy, start, stop, and reload web applications.

Tomcat Documentation

Another important link to browse is the link to the Tomcat documentation. Go to the Tomcat start page (http://localhost:8080/), and follow the link to Tomcat Documentation. These pages contain important information about how to configure Tomcat and deploy Web applications. The following shows the Tomcat Documentation Index page.

Tomcat Documentation Index Page

Under the Reference section, follow the link to the Servlet API Javadocs. These javadocs (API documentation generated from the source code using the javadoc tool) describe the API of the programming environment in which you will be working.

Log Files


When debugging Java Web applications, it is essential that you access the log files. Take a look at the contents of the logs directory within the Tomcat folder to see which log files Tomcat has produced so far. Some of these log files are written to directly by Tomcat and other files are written to from logging commands that appear within applications. For instance, the Tomcat server writes logging information into a file that starts with the word catalina and the manager application writes logging information into a file with a name that starts with the word manager. After initialization, Tomcat also writes logging messages for various events, such as when it deploys a new application or changes the state of an existing application.

All of the above log files may contain error messages. It is very important to resolve all errors that appear in these files.

For debugging purposes, it is useful to monitor the contents of these files dynamically. If you are working under linux or MacOS X, you can use the tail command to view changes made to a file. The following is an example running tail to monitor the state of a log file.

tail -f ${TOMCAT_HOME}/logs/catalina.out

Under Windows, there is an Eclipse plug-in called Logwatcher that enables similar functionality. The next section explains how to install and use LogWatcher.

Logwatcher Installation

If you are not in Windows, then you can use the tail command as described above and skip the installation of the LogWatcher plug-in.

Eclipse should not be running for the following procedure to work, so shut it down if it is. Go to Graysky Plugins For Eclipse and locate the most recent release of the logwatcher plugin. Extract the archive and copy the contents of the plugin folder into the dropins folder with your Eclipse installation. The plug-in will be loaded the next time you start (or restart) Eclipse.

LogWatcher Usage

To use logwatcher, you open a logwatcher view inside Eclipse. Do this by selecting Show View under the Window menu and selecting Other ... Log Files ... LogWatcher. To view a log file in LogWatch, click on the new icon in the LogWatcher window and browse to one of the Tomcat log files described previously.

Understanding Tomcat Class Loading

One of the most important things to be aware of when developing Java applications are class loading issues. Java uses a class called ClassLoader to perform the job of loading class definitions into memory, from which the JVM accesses static attributes and methods and creates object instances.

A Web container will utilize several instances of ClassLoader to perform class loading responsibilities. For example, a class loader is instantiated for each web application and used to load classes from jar files in WEB-INF/lib and class files under WEB-INF/classes. Another ClassLoader is used to load classes from jar files found in the lib directory of Tomcat. Other ClassLoaders are also used, but we do not go into any more detail than this.

A ClassLoader locates classes by searching a classpath. For a Java Web application running in Tomcat, the classpath from which class definitions can be loaded include the following directories, where ${project} is the name of the Eclipse project that contains the application and ** indicates recursive descent through all subdirectories.

The classes under the lib directory are shared by Tomcat and by all deployed web applications. The classes under ${project}/web/WEB-INF are only accessible by the Web application corresponding to ${project}.

Deep Restart of Tomcat


Sometimes it is necessary to perform a deep restart of Tomcat, which means to delete all of Tomcat's generated files, which are stored in the work directory. These files include JSP translated into source code and their corresponding class files. These files also include other derived objects related to the configuration of tag libraries and other features.

Difficult-to-find bugs will occur when the generated files in the work directory are not synchronized with the files comprising the installed web application. If you suspect that this condition may be causing problems at some point, you can force Tomcat to regenerate all files in the work directory, which is a procedure that we refer to as a deep restart of Tomcat.

Deep Restart Procedure

  1. Stop Tomcat.
  2. In the Eclipse log file watch window, close all log files.
  3. Delete the files within the directory ${TOMCAT_HOME}\logs.
  4. Delete the directory ${TOMCAT_HOME}\work.
  5. Start Tomcat.


(1) Servlet API

Locate and study the documentation for the HttpServlet class.

(2) Firewall Experiments

In your firewall system, close port 8080 to accepting connections from the outside world. On a separate computer, try to access tomcat (using the IP address of the tomcat server for URL), and verify that the connection fails. Then, open port 8080, and verify that you can connect to tomcat.

Note: if you are using a laptop on a university campus wireless network, the university may block connections between laptops. In this case, you will not be able to access tomcat running on another student's laptop.

(3) Log Files

Use LogWatcher or other log file viewer to observe how Tomcat writes to the catalina.out log file. While the log viewer window is open and the catalina log file is selected, start and stop Tomcat several times.

(4) Deep Restart

Perform a deep restart of tomcat.

back next

Copyright 2007-2009 David Turner and Jinseok Chae. All rights reserved.