Web Server Connectors
by Jeff Hunter, Sr. Database Administrator
In the previous section, we examined the HTTP connectors that allow Tomcat to work as a standalone web server. We can also use a web server such as Apache or IIS, along with Tomcat to serve up HTTP contents to the user's web browser. In this configuration, the web server and Tomcat communicate with each other using Tomcat's web server connectors.
At the time of this writing, there are two Tomcat connectors and protocols - WARP and AJP.
Reasons for Using a Web Server as opposed to Tomcat's Built In One
The obvious question at this point is - why do we need to use a separate web server when Tomcat already has a HTTP Connector? Here are some of the reasons:
Tomcat is inherently slower than a web server, and therefore we want the static content to be served up by the web server, while Tomcat handles the dynamic content (JSP pages and servlets). Passing requests for static HTML pages, images, and stylesheets through a servlet container written in Java is not very performant as compared to a web server.
A web server link Apache has been around for a lot longer than Tomcat and has far fewer security holes.
Apache is much more stable than Tomcat. In an event of a Tomcat crash, the entire web site will not come down - only the dynamic content served by Tomcat would be unavailable.
Apache is also far more configurable than Tomcat. For example, it supports virtual hosts, allowing a single Apache installation to host more than one web site. Using Apache as the front-end allows you to use its rich functionality.
- Additional functionality
Web sites often have legacy code in the form of CGI programs. They might also use scripting languages like Perl or Python to implement specific functionality. Web servers like Apache support CGI programs and have modules for Perl and Python - Tomcat does not. This is yet another reason why you would want to run a web server in addition (or better said, in front of) to Tomcat.
Web Server Connector Architecture
All the web server connectors work on the same principle - they have an Apache module end (mod_jk, mod_jk2, mod_webapp) written in C that gets loaded by Apache (or other supported web servers) the way any other Apache modules gets loaded.\ +-------------+ HTTP request/response +------------------+ | | | ---------------> | Web Server | | | Web Browser | <--------------- | (such as Apache) | | | | +------------------+ | +-------------+ ^ | | HTTP Request/ | | | Response | | | (WEB) +---------------------+ | (SERVER) | Connector module | | | (such as mod_jk, | | | mod_webapp) | | | Written in C and | | | loaded as a regular | | | Apache Module. | | +---------------------+ | ^ | / Protocol (WARP, AJP) | | Specific binary format | | \ +---------------------+ | | Connector | | | (Tomcat side) | | | Written in Java | | +---------------------+ | ^ | | Servlet Request/ | | | (TOMCAT) Response | | | | | | +---------------------+ | | Tomcat Servlet | | | Container | | +---------------------+ | /On the Tomcat end, each servlet container instance has a connector module component written in Java. In Tomcat 4.x, this is a class that implements the org.apache.catalina.Connector interface. The connector class for WARP is org.apache.catalina.connector.warp.WarpConnector and that for AJP version 13 is org.apache.ajp.tomcat4.Ajp13Connector.
The web server (generally Apache listening on Port 80) handles all requests for static content, as well as all non-servlet/JSP dynamic content (CGI scripts, for example). This is very important to recognize. Apache receives ALL traffic. Also remember that Tomcat (with its default connector) listens for HTTP requests on default port 8080.
Apache makes the decision of what to do with ALL traffic by use of the $APACHE_HOME/conf/httpd.conf file. Apache will always have a context path of "/" which is the $APACHE_HOME/htdocs directory and defined by the directive DocumentRoot. This will be the place where all "non-web" applications will be hosted from. This would include sites that are all static and/or contain legacy CGI code. Now, it is important to remember that a web application also contains static content like HTML and images.
In order for the Apache web server to serve up static content in a web application, insert an Alias in the httpd.conf file that points to the web application. This helps to serve up static content, but in order for Apache to hand off dynamic requests (Servlets and JSPs) to Tomcat it needs know what type of requests these are. This is typically done with the JkMount directive. All mod_jk directives (and the loading of the mod_jk.so Apache module) are typically included in the file $CATALINA_HOME/conf/auto/mod_jk.conf. This file is automatically created by the Tomcat server and can be Included in the httpd.conf file to declare all Tomcat (Servlet and JSP directives) but if you look closely at this file, it is not perfect. It is setup to pass ALL traffic (not only Servlets and JSPs but ALL traffic). As you will see below, we will make modifications to this file and include the modified version in the httpd.conf file.
Consider the following snippet:
<IfModule !mod_jk.c> LoadModule jk_module /u02/app/apache/modules/mod_jk.so </IfModule> JkWorkersFile "/u02/app/tomcat/conf/jk/workers.properties" JkLogFile "/u02/app/tomcat/logs/mod_jk.log" JkLogLevel info <VirtualHost cartman.ads.com> ServerName cartman.ads.com JkMount /examples/*.jsp ajp13 JkMount /examples/servlet/* ajp13 </VirtualHost>