by March 28, 2001 0 comments



JSP or Java Server Pages are dynamically generated pages similar to ASP (Active Server Pages) and PHP. JSP is based on Java, and is platform as well as Web server independent. If a Web server doesn’t have built-in support for JSP, you can add the same to it. In this article, we’ll look at Tomcat, a JSP and servlet engine that lets you add JSP support to your Web server. We’ve given it in this month’s PCQuest CD in the /cdrom/linux directory.

The Tomcat documentation calls it “a Servlet container with a JSP environment”. Simply put, it’s a program that can process JSP files and deliver the resulting dynamic content. Tomcat can work as a standalone or with a Web server like Apache. In this article, we’ll set up Tomcat on a PCQ Red Hat 6.2 Linux for both configurations.

Standalone Tomcat

In this setup, Tomcat will act as a simple Web server that’ll only process JSP pages. For this, it requires the Java Development Kit (JDK) for Linux. So, let’s first install the JDK on Linux, which you’ll find on this month’s CD.

Mount the CD and go to the directory cdrom/demand. Copy the file ‘j2sdk-1_3_0_02-linux-rpm.bin..bin’ to a directory, say, /opt. Change to /opt directory and execute the file. The commands involved are:

mount /mnt/cdrom cd
/mnt/cdrom/cdrom/demand
cp j2sdk-1_3_0_02-linux-rpm.bin..bin /opt
cd /opt
./j2sdk-1_3_0_02-linux-rpm.bin..bin

On executing the last step mentioned above, you’ll be presented with a license agreement. Type ‘Yes’, and an RPM file named ‘j2sdk-1_3_0_02-linux.rpm’ is created in the same directory. Now install this rpm as:

rpm -ivh j2sdk-1_3_0_02
linux.rpm

This will install the JDK for Linux in /usr/java/jdk1.3.0_02 directory.

Now, we proceed to installing Tomcat. Copy the file ‘jakarta-tomcat-3.2.1.tar.gz’ from the linux directory on the CD to /opt directory and extract this tarred and compressed archive:

tar -zxvf jakarta-tomcat
3.2.1.tar.gz

This will create a directory named jakarta-tomcat-3.2.1 under /opt. Rename this directory to tomcat for convenience:

mv /opt/jakarta-tomcat
3.2.1 /opt/tomcat

Now we must set two environment variables namely JAVA_ HOME and TOMCAT_HOME to point to the JDK and Tomcat directory respectively:

export JAVA_HOME=/usr/java/jdk1.3.0_02/
export TOMCAT_HOME=/opt/tomcat

You’ll need to append these lines in a Linux startup file like /etc/profile so that these variables are set during subsequent reboots.

Now to run Tomcat as a standalone server. To start Tomcat, change to the directory /opt/tomcat/bin and execute the startup.sh file as:

./startup.sh

After flashing a couple of messages on the command line Tomcat starts and listens to port 8080. You’ll find a number of ready-to-use JSP examples located in the subdirectory webapps/examples/ jsp. To run one of these examples say the ‘numberguess’, key in the following URL in your Netscape browser:

http://localhost:8080/eamples/jsp/num/numgues.jsp

and follow the game instructions in the Web page. If you know how to write JSPs, you can put your JSP pages in the directory /opt/tomcat/examples/jsp and access them with the URL:

http://localhost:8080/e
amples/jsp/ your-JSP-file>

Tomcat with Apache

Tomcat can work with a Web server like Apache, processing only JSP files, while leaving the job of delivering static or dynamic contents of other scripting languages like Perl, PHP, or ASP to the Web server. This is useful because Tomcat is not developed to be a full-fledged Web server, and is not as fast and configurable as a Web server. In this section, we’ll set up Tomcat as a module for Apache. We assume that you have a working Apache installation. You must also install the Apache development RPM, which you can find on the PCQuest June 2000 CD. Change to the RedHat/RPMS directory on the CD and install the RPM as:

rpm —ivh apache-devel*

Copy the file ‘jakarta-tomcat-3.2.1-src.tar.gz’ from the linux directory on this month’s CD to your /opt directory. Untar and uncompress the archive as you did earlier. This would create a directory named ‘jakarta-tomcat-3.2.1-src’ under /opt. Change to the subdirectory ‘src/native/apache/jserv’ within this directory and issue the following command:

apxs -c —o mod_jserv.so 
*.c 

This would create an Apache module mod_jserv.so in the same directory. Now copy this file to the modules directory of Apache as:

cp mod_jserv.so 
/etc/httpd/modules

Next, create a configuration file named tomcat.conf under the directory /etc/httpd/conf using a Linux text editor and key in the following lines. Basically, this specifies the location of JSP files to Apache, and makes Apache hand over files with JSP extension to Tomcat.

LoadModule jserv_module modules/mod_jserv.so
<IfModule mod_jserv.c>
ApJServManual on
ApJServDefaultProtocol ajpv12
ApJServSecretKey DISABLED
ApJServMountCopy on
ApJServLogLevel notice
ApJServDefaultHost localhost
ApJServDefaultPort 8007
Alias /examples /opt/tomcat/webapps/examples
<LocationMatch /examples/*.jsp>
SetHandler jserv-servlet
</LocationMatch>
ApJServMount /examples/servlet /examples
</IfModule>

Now open the file httpd.conf found in the same directory and append the following line in it:

Include conf/tomcat.conf

If Tomcat is not running, start it by executing the startup.sh file. Restart Apache, so that it becomes aware of the new configurations:

/etc/rc.d/init.d/httpd restart

Now to run the same numguess.jsp file, fire up Netscape and key in the following URL:

http://localhost/examples/jsp/num/numguess.jsp 

Note that this time we are not giving the port number 8080, since the request is going to the standard port 80 where Apache is running. When Apache sees a file with JSP extension, it hands it over to Tomcat. Tomcat, after processing the JSP file, delivers it back to Apache, which in turn is passed to the Web browser for display.

Next month, we’ll explore the Tomcat-Apache configuration in more detail and learn to write our own Java Server Pages.

Shekhar Govindarajan

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.