Using Java Servlets with Apache

Using Java Servlets with Apache

Servlets are Java programs that execute in a Web server in response to requests from a Web browser. Servlets are more powerful than CGI programs commonly used as gateways between Web servers and other services such as databases. One drawback of CGI programs is that the Web server has to start a new process to run a standalone CGI program whenever the server receives data from an HTML form.

Servlets provide functionality similar to that of CGI programs, but servlets avoid the overhead of new process startup. Typically, the Web server runs a Java Virtual Machine (JVM), and that JVM, in turn, loads and runs the servlets. The JVM runs a servlet in a Java thread, which is faster and requires fewer operating system resources than a new process. Additionally, the JVM can keep a servlet loaded and running so that a single instance can handle many future requests. This makes servlets more responsive than other CGI programs, especially ones written in scripting languages such as Perl. At the same time, servlets are portable to any system that supports Java, just as Perl CGI programs can run on any system with a Perl interpreter.

Many websites already use servlets extensively. While browsing the Web, you may have accessed some servlets without even realizing it. A giveaway that the Web server is using a servlet is the occurrence of /servlet/ in the URL (in the same way that /cgi-bin/ commonly appears in URLs that refer to CGI programs).

Many websites use another Java-based approach to generate dynamic content. That approach is based on what is known as JavaServer Pages (JSP) technology. A Web page based on JSP uses XML tags and Java scripts along with other HTML tags. Typically, a JSP page has the .jsp file extension, which tells the Web server that the page should be processed by a server that understands JSP.

If you want to support Java servlets and JSP on your Apache Web server, you have to download and install the following software on your Red Hat Linux system, in the order shown:

  1. Java Development Kit (JDK) release (version 1.2 or later)-For writing Java servlets. Download the binary distribution for Linux from http://java.sun .com/j2se/downloads.html. You can download the RPM distribution, which is easy to install with the rpm command (after an additional step of unpacking). After installing JDK, set the environment variable JAVA_HOME to the pathname of the directory where you installed the JDK release.

  2. Apache Tomcat-A server that runs servlets and can execute JSP documents. Tomcat version 5.0 implements the Servlet 2.4 and JSP 2.0 specifications from the Java Community Process ( More information about Tomcat is available from the home page of the Apache Jakarta project at Download the Tomcat binary distribution from You should download what the Jakarta project calls Release Builds because they are considered ready to use by anyone.

  3. Jakarta-Tomcat-Connector mod_jk-An Apache Web server module that enables the Apache Web server to communicate with Tomcat. Download the latest source files from Online information about the jk connector is available at connectors/jk/doc/.


    You also need the Apache httpd source files before you can build the mod_jk module.

The following sections show how to install JDK, Tomcat, the Apache httpd source RPM, and build the mod_jk2 module that connects the Apache Web server to Tomcat.

Installing the Java 2 Software Development Kit

After you download and save the Linux RPM binaries for Java, you'll see a file with a name ending in .bin. You have to go through an extra step before you can use RPM commands to install this file.

Insider Insight 

To make sure that this definition is available to everyone who use the Bash shell, create a text file named in /etc/profile.d directory and add the following lines in that file:

export JAVA_HOME=/usr/java/j2sdk1.4.0_03

The /etc/profile script will then read and execute the script at login and set the JAVA_HOME environment variable and add the JDK binaries to the PATH.

Cross Ref 

To learn about Java programming on Red Hat Linux, consult Chapter 26.

Installing Apache Tomcat

Download the latest binary for the Release Build of Tomcat. If you have JDK 1.4, you should download the file with a name that ends in -LE-jdk14. That's the lightweight binary that does not include the XML parser-the XML parser is already included in the 1.4.x versions of Java.

To install and test the Tomcat binaries, log in as root and follow these steps:

  1. Copy the downloaded Tomcat compressed tar archive to a suitable directory such as /usr/local with the following command:

    mv tomcat-*LE-jdk*.gz /usr/local
  2. Unpack with the tar zxvf command in its new location (for example, /usr/local) with the following commands:

    cd /usr/local
    tar zxvf tomcat-*.tar.gz
  3. Set the CATALINA_HOME environment variable to the full pathname of the directory where you installed Tomcat. For example, I installed version 4.1.18 of Tomcat in the directory /usr/local/jakarta-tomcat-4.1.18-LE-jdk14, so I define CATALINA_HOME as follows:

    export CATALINA_HOME=/usr/local/jakarta-tomcat-4.1.18-LE-jdk14

    You need the CATALINA_HOME environment variable in the future. Therefore, you should create a file named in /etc/profile.d directory with the following line in that file:

    export CATALINA_HOME=/usr/local/jakarta-tomcat-4.1.18-LE-jdk14

    Then, whenever you log in, the CATALINA_HOME environment variable would be defined.

  4. Start Tomcat with the following commands

    cd $CATALINA_HOME/bin
  5. To see if Tomcat is working, start a Web browser on your system or another system on the LAN. Point the browser to your system at port 8080 by using the following URL:


    If Tomcat is up and running, you should see three directories named images, jsp, and servlets. Browse through the jsp and servlets directories and try out the JSP and servlet examples.

  6. Shut down Tomcat with the following command:

    cd $CATALINA_HOME/bin

Now, you can proceed to install the Apache httpd source files and then go on to build the mod_jk connector module.

Installing the Apache httpd Source RPMs

To build mod_jk from source files, you also need the Apache httpd source files. You can download the source files in the form of an RPM from one of the sites listed in Click one of the Distribution links, and then click the folder for the Red Hat Linux version (for example, 8.0). You will find the source RPMs in the SRPMS folder corresponding to your language (en for English) and processor architecture (i386 for Intel). For example, for Red Hat Linux 8.0, I found the source RPM for Apache httpd 2.0.40 in the 8.0/en/os/i386/SRPMS directory of the download site.

The source RPM is about 4.5MB in size. Download it and use the following command to install on your Red Hat Linux system:

rpm -ivh httpd-*.rpm

After you install the RPM, you find a compressed tar file, as well as several other files, in the /usr/src/redhat/SOURCES directory. For example, for Apache 2.0.40, the source archive is named httpd-2.0.40.tar.gz.

Unpack the file with the following command:

tar zxvf httpd-2.0.40.tar.gz

This creates a subdirectory named httpd-2.0.40 and places the Apache httpd source files, organized into further subdirectories. Type the following commands to configure the Apache httpd directory:

cd httpd-2.0.40

That's all you have to do. To build mod_jk, all you need is the full pathname of the directory where the Apache httpd source files are located. For example, in my case, the directory name is


Building the mod_jk Module

You have to download and build the mod_jk module (also referred to as JK) before you can use Tomcat with the Apache httpd server. Point your Web browser to http:// and download the latest source files for JK. For example, for version 1.2.2 of JK, the source file is at v1.2.2/src/. From this location, I downloaded jakarta-tomcat-connectors-jk-1.2.2-src.tar.gz, which is about 559K in size.

After downloading the source archive, follow these steps to build the file:

  1. Log in as root and copy the file to a suitable directory. I moved it to /usr/local directory with the command:

    mv jakarta-tomcat-connectors-jk-1.2.2-src.tar.gz /usr/local
  2. Unpack the file in the /usr/local directory by typing commands such as the following:

    cd /usr/local
    tar zxvf jakarta-tomcat-connectors-jk-1.2.2-src.tar.gz

    The files are now in /usr/local/jakarta-tomcat-connectors-jk-1.2.2-src directory.

  3. Type the following commands:

    cd jakarta-tomcat-connectors*
    cd jk/native

    This will create a file called configure in that directory.

  4. Run the configure file with the following command:

    ./configure --with-java-home=${JAVA_HOME} --with-apache=/usr/src/redhat/SOURCES/httpd-2.0.40
  5. Type the following commands to build module:

    cd apache-2.0
  6. Type the following command to copy the module to the location where all Apache httpd modules are stored:

    cp /usr/lib/httpd/modules

Now, all you have to do is configure Apache httpd, Tomcat, and mod_jk to complete the connection between Apache httpd and Tomcat.

Connecting Apache httpd Server to Tomcat

The first step is to configure Tomcat. Edit the server.xml file located in the $CATALINA_HOME/conf directory (you should define the CATALINA_HOME environment variable as the directory where Tomcat is installed). Make the following changes in $CATALINA_HOME/conf/server.xml:

  • Look for a line that begins with <Server port="8005". Add the following directly below that line:

    <Listener className="org.apache.ajp.tomcat4.config.ApacheConfig" modJk="/usr/lib/httpd/modules/" /> 

    Make sure that the last pathname matches the directory where you have placed the file.

  • Locate the line that starts with <Host name="localhost". Add the following immediately after this line:

    <Listener className="org.apache.ajp.tomcat4.config.ApacheConfig" append="true" forwardAll="false" modJk="/usr/lib/httpd/modules/" /> 

    Make sure that the last pathname matches the directory where you have placed the file.

  • Change the name parameter in the <Host name="localhost" line from localhost to whatever name you have used in the ServerName directive in the Apache httpd.conf file.

Next, edit the Apache httpd configuration file-/etc/httpd/conf/httpd.conf-as follows:

  • Make sure that the name specified with the ServerName directive matches the name used in the <Host name= line in Tomcat's server.xml file.

  • At the very end of the configuration file, add the following line:

    Include /usr/local/jakarta-tomcat-4.1.18-LE-jdk14/conf/auto/mod_jk.conf

    Make sure that the first part of the directory name-/usr/local/jakarta- tomcat-4.1.18-LE-jdk14-matches the definition of the CATALINA_HOME environment variable (the directory where you installed Tomcat).

    Insider Insight 

    Everytime Tomcat starts, it creates the mod_jk.conf file referenced in the last line in httpd.conf file. Thus, the configuration of mod_jk module is all done automatically.

The final configuration step is to create the file in the $CATALINA_HOME/conf/jk directory (if the directory does not exist, first create it). Here's an example of that file from my installation (the comments tell you what you have to change):

# workers.tomcat_home should point to the location where you
# installed Tomcat. Change as necessary.

# workers.java_home should point to your Java installation. Change
# as needed.
# Workers to be created
# Define a worker named ajp13 and of type ajp13.
# Note that the name and the type do not have to match.
# Make sure the hostname matches what's in ServerName (httpd.conf)
# and Host name (server.xml). Change if necessary.

Testing Apache httpd with Tomcat

To test whether Apache httpd is working with Tomcat, perform the following steps:

  1. Log in as root and start Tomcat with the following command:

    cd $CATALINA_HOME/bin
  2. Wait 10 seconds or so for Tomcat to start up.

  3. Start Apache httpd with the following command:

    service httpd start
  4. Open a Web browser on the same system and point the Web browser to the URL http://localhost:8080/examples. You should see the jsp and servlets directory and be able to execute the examples. This shows that Tomcat is working correctly.

  5. Now, type the following URL in the Web browser:


    You should again see the same directories as you did when you connected to port 8080. This shows that Apache httpd is correctly handing over the JSP and servlet requests to Tomcat.