In the article Tomcat on Linux (page 105, April 2001) we saw how to connect the Apache Web server to Tomcat 3 using the mod_jserv connector. In Tomcat 4 on PCQLinux 7.1 (page 66, September 2002), we used a connector called mod_webapp to connect Apache and Tomcat 4. Throughout the series of Tomcat articles (starting September 2002) we have been using the mod_webapp connector. We wind up the series with this article, which explains setting up another connector called mod_jk2 for Tomcat 4 and Apache 1.3, running on PCQLinux 7.1.
Before getting into the setting up process, let’s understand what connectors are and why there are more than one of them.
Tomcat is developed to be a JSP and Servlet engine. Though it can deliver static HTML and image files over the Web, it is not optimized for it. Static Web content is best delivered by Web servers like Apache. This is where connectors called JTC (Jakarta-Tomcat-Connectors) come into the picture. Connectors are small programs called modules, as suggested by the mod prefix in their name. They are used by a Web server to communicate with Tomcat.
Each connector follows a different protocol (like AJP, WARP) to accomplish this connection. The connectors can be configured in such a way that whenever the Web server comes across a JSP or a Servlet, it can hand over their processing to the Tomcat engine.
After processing, Tomcat will hand over the output of the JSP/Servlet back to the Web server, which is in turn delivered to the client (Web browser).
mod_jserv is one such connector that can be used with Tomcat 3, but it is now obsolete. The recent connectors available for Tomcat 4 are mod_jk and mod_webapp. mod_jk2 is the latest version of
mod_jk.
Unlike mod_webapp, mod_jk2 supports load balancing and multiple Web servers like Microsoft IIS, Netscape iPlanet and Lotus Domino, besides Apache.
Instead of all Web content, mod_jk2 can be used to set up Apache to hand over only JSP and Servlets (on the basis of file extension) to Tomcat.
This is not possible with mod_webapp. The choice of connectors depends on the need for advanced features (besides simple connectively) and versions of Tomcat and the Apache Web server
being used.
Pre-requisites
We assume that you have set up Tomcat on PCQLinux 7.1 as explained in the September 2002 article. Download the binary module of mod_jk2 (a few KB download) from
http://jakarta.apache.org/ builds/jakarta-tomcat-connectors/
jk2/release/v2.0.1/bin/linux/i386/mod_jk2-1.3-eapi.so
Copy the downloaded file named mod_jk2-1.3-eapi.so to the directory /etc/httpd/modules on the PCQLinux 7.1 machine.
Remove mod_webapp
If you are using mod_webapp as per our Tomcat series of articles, you must remove it before setting up mod_jk2.
Open the file named httpd.conf found in the directory /etc/httpd/conf in a Linux text editor. Delete the following lines related to mod_webapp from
httpd.conf.
LoadModule webapp_ module modules/mod_webapp.so
AddModule mod_webapp.c
WebAppConnection conn warp localhost:8008
If you have set up ‘examples’ and ‘guestbook’ (see Web Applications with Tomcat, page 75, PCQuest November 2002) Web applications with Apache, then you must also remove the following lines in
httpd.conf.
WebAppDeploy examples conn /examples
WebAppDeploy guestbook conn /guestbook
Setup mod_jk2
Append the following line to httpd.conf.
LoadModule jk2_module modules/mod_jk2-1.3-eapi.so
Create a new file called workers2.properties and type in the following lines in it.
file=${serverRoot}/logs/jk2.shm
size=1000000
Save this file in the directory /etc/httpd/conf. If Tomcat is not already running, run it as follows.
/opt/tomcat/bin/startup.sh
Restart Apache Web server as
/etc/rc.d/init.d/httpd restart
Set up Web applications
To access the ‘examples’ Web application that comes with Tomcat, through Apache, append the
following line to workers2.properties file.
Similarly, to access our GuestBook Web application, append the following line.
Restart the Apache Web server. You should be able to access the number guess JSP application using the URL http://127.0. 0.1/examples/
jsp/num/numguess.jsp
Note the missing port number 8080 in the above URL, which means the request is going to directly to the Apache Web server, in place of Tomcat. Similarly, you can access the guestbook Web application with the URL http://127.0.0.1/
guestbook/guestbook.jsp
Serve JSP and Servlets
With the lines
In this case, Apache will contact Tomcat even if the request is for a static HTML file or an image file. But, you can set up Apache to contact Tomcat only to serve JSP and Servlets. To accomplish this, modify the lines
in workers2.properties to
If the Web application contains Servlets, then you must also set up a
Web applications contain Servlets.
Hence, for them to work properly, you must append the following lines to workers2.properties.
Now, you can create directories with the same names as the Web application under /var/www/html and place all the static
Web contents there.
As an example, create a directory named guestbook under /var/www/html and save the following contents as launch.htm, in the guestbook directory:
Click here to launch the guestbook web application
Next, key in the following URL in a Linux Web browser. http://127 .0.0.1/guestbook/launch.htm
The browser will contact Apache, which will readily serve launch.htm from the directory /var/www/html/guestbook. However,
when you click on the hyperlink ‘here’, the requested absolute URL becomes http://127.0.0.1/
guestbook/guestbook.jsp
This time Apache will contact Tomcat to serve the file with the .jsp extension.
For more on the Tomcat connectors, see http://jakarta.apache.org/ tomcat/tomcat-4.1-doc/config/
connectors.html.
Shekhar Govindarajan