Apache Tomcat / JSP
— print (last updated: Jun 21, 2009) print

Select font size:
Personalize this online document by providing values relevant to you. Replace the generic LOGIN by the actual login:
   
The Apache web server does not have support for execution of Java servlets and server pages, but it can be connected to a dedicated server which can handle these. There are a number of such servers, but we're interested in the Apache open source Tomcat server:
http://tomcat.apache.org
Ubuntu supports both "current" version of Tomcat, version 5 and 6. We will use version 6. The Ubuntu installation is:
$ sudo apt-get install tomcat6 tomcat6-admin tomcat6-examples tomcat6-docs
Tomcat starts automatically and, by default, uses port 8080. Internally, one or more Java virtual machines handle browser requests by spawning program threads. Each request uses byte-compiled code created at run-time for the Java Server Pages. The whole setup is very fast and efficient. Once you have Tomcat started, view the Tomcat root URL:
http://localhost:8080/

Using the Tomcat Manager

The Tomcat manager feature allows you to dynamically deploy, undeploy, start, stop and restart web applications through a web interface. In order to set up access to this service, you must define a manager role and set a user/password combination which is used to validate access to this role. Toward this end, edit the file (as root):
/etc/tomcat6/tomcat-users.xml
Within the tomcat-users start and end tags (outside of the comments), add the following (you can make the values of username and password anything you'd like):
<role rolename="manager"/>
<user username="admin" password="adminfoo" roles="manager"/>
Restart tomcat by:
# /etc/init.d/tomcat6 restart
Access the manager from this URL:
http://localhost:8080/manager/html
Look in the Administration section on the left-hand side and select the Tomcat Manager hyperlink. Clicking on this hyperlink will require you to give the user/password combination just entered to the authentication popup window:
user:      admin 
password:  adminfoo
The manager application displays currently deployed web applications in the central table of the List Applications page; there are also the features: HTML Manager Help, Manager Help, and Server Status.

In Deploy section at the bottom you can deploy web applications dynamically either through a URL to the application directory, or via a WAR (Web ARchive) file. If you know about Java JAR files, a WAR file is simply JAR files which contain the additional web control information.

User-based installation for Eclipse

For actual development of Java Web applications, Eclipse IDE with Web Tools packages installed works very well. Eclipse can export its Web projects directly as WAR files which can then be imported into the system-wide tomcat.

It's best not to deal with the system-wide tomcat directly, so download and install the latest tomcat (here on the Computer Science server):
apache-tomcat-6.0.20.zip
You can ignore the minor version difference between this version and the system version. You can install it anywhere in within your home directory. I like to put it in bin to ensure that I won't inavertently delete it. Assuming you've downloaded to the Desktop:
$ unzip ~/Desktop/apache-tomcat-6.0.20.zip -d ~/bin

Open Eclipse. We will install our local tomcat server in Eclipse.
  1. Open the J2EE perspective by going through Window Open Perspective Other
  2. Right-click in the Project Explorer window. Select New Other. Open the Server category, select the Server entry within and click Next.
  3. In the Define a New Server window, select Apache Tomcat v6.0 Server, click Next.
  4. In the Tomcat Server window, click the Browse button to specify the Tomcat installation directory, ~/bin/apache-tomcat-6.0.20. Click Finish.
Back in Eclipse, you should see the Tomcat 6.0 server at localhost entry listed in the servers tab. Double-click on this entry to reveal the configuration listing. Shift to the right to find these settings:
Tomcat admin port  8005
HTTP/1.1           8080
AJP/1.3            8090
These all conflict with the system-wide tomcat ports, so we want to avoid the conflict. The easiest way is to add a "1" in front of each port, getting:
Tomcat admin port  18005
HTTP/1.1           18080
AJP/1.3            18090
Save the configuration and close this configurator.

Sample JSP Application

  1. Select New Project Web Dynamic Web Project; click Next. Set HelloJSP as the Project Name; click Finish.
  2. Right-click on the HelloJSP project and select New JSP. Give it the name index.jsp. Click Finish.
  3. It looks like HTML code! We will modify the code to give it some JSP-specific features. Add this to the body and the save it.
    First example:
    <% out.println( "Hello World 1" ); %>
    <br />
    Second example:<br />
    <% String s = "Hello World 2"; %>
    Here it is: <%= s %>
    
    The web tags <%, %> and <%= are the special JSP server-side execution tags, equivalent to the Php versions with the special "element" % replacing ?.
  4. Run this web application (with only one file) by clicking the green run button, or by right-clicking on the project and selecting Run As Run on Server.
  5. A Run On Server popup queries you for the server to use. Check the Always use this server when running this project checkbox and click Finish. The tomcat server should start and you should see the JSP page displayed in Eclipse's internal browser:
    First example: Hello World 1
    Second example: 
    Here it is: Hello World 2 
    
    If the server output fails, go to the Servers window, right-click on the Tomcat server entry and select Restart.
Alternatively, you can see this actived from this (Firefox) browser:
http://localhost:18080/HelloJSP/

Deploy in system tomcat

Right-click the HelloJSP project, then Export WAR file. In the popup, browse to select the destination, the default being /home/LOGIN/HelloJSP.war

Access the system tomcat manager from this URL:
http://localhost:8080/manager/html
At the bottom, in the WAR file to deploy section, click the Browse... button and navigate to select the WAR file we just created.

After choosing this file, click the Deploy button just below the field. If it went right (which it should), you should see "OK" in the Message section at the top of the page. You should also see the new context (web application) "/HelloJSP" appear in the list.

Then activate /HelloJSP by clicking on it in the leftmost column of the List Applications table to get this URL:
http://localhost:8080/HelloJSP/

Modifying the code

If any of the code in the JavaWeb application is modified you must recreate the archive, undeploy the old one and finally re-deploy the new one to pick up the changes.

Apache Proxy access

The tomcat service can be accessed through regular http (port 80) protocol using proxy service. Here's how to do it:
$ sudo a2enmod proxy proxy_http
Create the following Apache configuration file:
/etc/apache2/conf.d/proxy.conf
with this content (I did not have time to check every detail about what is absolutely necessary):
ProxyRequests On
ProxyVia On

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass        /HelloJSP   http://localhost:8080/HelloJSP
ProxyPassReverse /HelloJSP   http://localhost:8080/HelloJSP

ProxyPass        /manager http://localhost:8080/manager
ProxyPassReverse /manager http://localhost:8080/manager
then reload Apache and try the URL's:
http://localhost/HelloJSP/
http://localhost/manager/html
Note that, in the case of the manager, the "links" to other applications will not work, but the manager itself will work in terms of deploying WAR files, etc.

The Tomcat JK connector service

This proxy scheme above works, but what we should really be using is is the so-called Tomcat JK connector service.
http://jakarta.apache.org/tomcat/connectors-doc/
A connector service provides the mechanism by which the Apache server, running on port 80, can effectively "call" the Tomcat server running on port 8080 for certain URLs. The mechanism involves a specific worker protocol, called ajp13 and an additional port (the default is 8009) for forwarding requests.

Ubuntu has the software packages libapache2-mod-jk which presumably should work right away, although there is some suggestion in these packages that they only work for tomcat 5. Try as I may, I cannot seem to get any version of this to work with the system tomcat.

This is essentially what works on Fedora

If you want to try to solve this problem, first make sure there is no conflict. Edit /etc/apache2/conf.d/proxy.conf and comment out the ProxyPass and ProxyPassReverse lines (put a "#" in front). Then reload Apache and perhaps double-check that the tomcat URLs only work when port 8080 is in use.

On Fedora it all works out readily with JK installed from source, so it seems like a neutral approach for any Linux. You'll need the Apache development package to do so:
$ sudo apt-get install apache2-prefork-dev
One bothersome issue about Apache2 is that it has two "modes", prefork and threaded. Ubuntu requires the prefork version to run Php, but there is a lingering sense that the threaded version may be necessary (??).

First of all you have to ensure that tomcat is serving port 8009. Edit the file
/etc/tomcat6/server.xml
Look for the line:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
which is inside the comments. Move it outside the comments and restart tomcat6:
$ sudo /etc/init.d/tomcat6 restart
In Fedora, there is no need to do this last step, 8009 is already open in its config file.

Next download the JK source. A recent is available from the Computer Science website as:
tomcat-connectors-1.2.28-src.tar.gz
Extract the tar ball somewhere:
$ tar xzf tomcat-connectors-1.2.28-src.tar.gz
obtaining the directory:
tomcat-connectors-1.2.28-src
Then compile and install as follows:
$ cd tomcat-connectors-1.2.28-src/native
$ ./configure --with-apxs=/usr/bin/apxs2
$ make
$ sudo make install
The only thing installed the Apache module:
/usr/lib/apache2/modules/mod_jk.so
Create a startup configuration file:
/etc/apache2/conf.d/mod_jk.conf
with this content:
# mod_jk configuration
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so

JkLogFile  /var/log/apache2/mod_jk.log
JkLogLevel info

JkMount /manager/    ajp13
JkMount /manager/*   ajp13

JkMount /HelloJSP     ajp13
JkMount /HelloJSP/*   ajp13
The last two line groups make the connection between Tomcat and Apache for this specific URLs "/manager" and /HelloJSP and all their descendants. After creating this file, restart Apache
# /etc/init.d/apache2 restart
Test the connector service by these URLs:
http://localhost/HelloJSP/
http://localhost/manager/html/


© Robert M. Kline