How to Use Apache Tomcat to Run Java Servlets on Your Computer - dummies

How to Use Apache Tomcat to Run Java Servlets on Your Computer

By Doug Lowe

Unfortunately, you can’t run Javas ervlet programs on any old computer. First, you have to install a special program called a servlet engine to turn your computer into a server that’s capable of running servlets. The best-known servlet engine is Apache Tomcat, which is available free from the Apache Software Foundation. Here, the early-release Tomcat version 8 was used.

Tomcat can also work as a basic web server. In actual production environments, Tomcat is usually used in combination with a specialized web server, such as Apache’s HTTP Server.

How to install and configure Tomcat

Installing Tomcat isn’t rocket science, but it’s not as easy as making toast, either. Here are the steps you can follow to set up Tomcat 8:

  1. Download the Tomcat Zip file from the Apache website.

    Although Apache also offers an executable setup file for installing Tomcat, download the Zip file instead.

  2. Extract the contents of the Zip file by right-clicking the file and choosing Extract All; then specify c: as the location to extract the files to.

    You probably don’t want to clutter your root directory with a bunch of files, but the Tomcat Zip file contains a single folder named apache-tomcat-8_0_0 (the version number may vary), so only this one folder is created. After all the files are extracted, give this folder a new name that’s a little easier to type.

  3. Create an environment variable named JAVA_HOME that points to the location of your Java Development Kit (JDK).

    To create an environment variable, open Control Panel, double-click the System icon, and then click Advanced System Settings→Environment Variables→New, and create a variable named JAVA_HOME. The value of this variable needs to be the complete path to your JDK installation folder, such as c:Program FilesJavajdk1.8.0.

    A common mistake is to set this variable to the bin directory or to the directory for the Java Runtime Environment (JRE), not the JDK. If Tomcat doesn’t start up later, double-check the JAVA_HOME directory.

  4. Copy the servlet-api.jar file to the jrelibext folder in your JDK root.

    If your JDK is installed in c:Program FilesJavajdk1.8.0, copy this file to c:Program FilesJavajdk1.8.0jrelibext. You find the servlet-api.jar file in c:tomcatlib, assuming that you extracted the Tomcat files to c:tomcat.

    If you skip this step or copy the servlet-api.jar file to the wrong place, you can’t compile your servlet programs. If you get compiler messages complaining that the javax.servlet package doesn’t exist, double-check this step to make sure you performed it right.

Starting and stopping Tomcat

After you install and configure Tomcat, you can start it by opening a command window, changing to the c:tomcatbin directory, and typing startup. A batch file runs, starting Tomcat. When Tomcat starts, it opens a second command window that displays various status messages.


You know that Tomcat has started when you see a line such as the following, indicating how long the startup took:

INFO: Server startup in 1186 ms

If the Tomcat window appears briefly, and then an exception message flies by quickly and the window closes, the most likely problem is that you already have a web server running on your system, and that server has already laid claim to the port Tomcat wants to use for HTTP communication. The solution to that problem is to edit the server.xml file in c:tomcatconf and look for this tag:

<Connector port="8080" ... />

Change the port number from 8080 to some other number, such as 18080. Later, when you display servlets in a browser window, you have to specify this number as the HTTP port number instead of 8080.

You don’t need to shut down Tomcat after you start it unless you make a change in one of its configuration files. If you do, you can shut down Tomcat by running the shutdown batch file from the c:tomcatbin directory. Then you can run the startup batch file to get Tomcat going again.

Testing Tomcat

To find out whether you installed Tomcat correctly, you can try running the test servlets that are automatically installed when you install Tomcat. Open a web-browser window, and type this address:


(If you changed the port number by editing the server.xml file, use the port number you specified instead of 8080.)

This page appears.