Customizing Tomcat

author-image
PCQ Bureau
New Update

In this article, we’ll see how to customize Tomcat. We’ll use the JPSs that we wrote in Write Your Own JSP Script, page 144 in this issue, place them under the directory
/home/httpd/html/jsp, and make Tomcat aware of this new location, called context in Tomcat lingo. Contexts are defined in the file server.xml found in tomcat/conf directory. Open this file in a text editor and go to the section named ‘Special webapps’. Here you can find the examples context:


docBase=”webapps/examples”

crossContext=“false”

debug =“0”

reloadable=”true” >

The two important attributes to understand are ‘docBase’ and ‘reloadable’. With docBase you can specify a directory relative (as above) to the Tomcat directory or an absolute path like
/home/httpd/ html/jsp. If the reloadable attribute is set to true, Tomcat will reload JSP on each request, a feature that can be used for testing. Obviously, reloading the JSP each time increases execution time. The ‘debug’ attribute can be used to increase the amount of debugging information shown.

Now below these append the following lines, where we define a context named ‘/jsp’ as:


docBase=”/home/httpd/html/jsp”

reloadable=”true” >

If you have set up Tomcat to run as standalone, then you just need to shutdown and restart it. Create the directory ‘jsp’ under
/home/httpd/html and place the file jspprogram.jsp in it. Now execute the JSP in the browser using the URL:

http://localhost:8080/jsp/jspprogram.jsp

If you have set up Tomcat as an Apache module, you must tell Apache to send all the files, with JSP extension, found in
/home/httpd/html/jsp directory to Tomcat. Also we must mount the /jsp context using ApJServMount directive. These changes are to be made in the tomcat.conf file that we created in
/etc/httpd/conf directory last month. After the changes tomcat.conf file will look like:

LoadModule jserv_module modules/mod_jserv.so



ApJServManual on

ApJServDefaultProtocol ajpv12

ApJServSecretKey DISABLED

ApJServMountCopy on

ApJServLogLevel notice

ApJServDefaultHost localhost

ApJServDefaultPort 8007

Alias /examples /opt/tomcat/webapps/examples

Alias /jsp /home/httpd/html/jsp



SetHandler jserv-servlet



SetHandler jserv-servlet

ApJServMount /examples/servlet /examples

ApJServMount /jsp

The first line LoadModule jserv_module modules/mod_ jserv.so loads the module mod_jserv.so for Apache, which we got by compiling the Tomcat sources (See Tomcat on Linux, page 105, PCQuest April 2001) from the directory modules (a subdirectory in
/etc/httpd). The path specified here is relative to /etc/httpd directory. If the module loading was successful, which is checked by the line mod_jserv.c>, the statements between this line and the line are executed. 

The next four lines have to be left at default. The line ApJServManual on means that the Tomcat engine must be started manually (we do this by executing
startup.sh). The line “ApJServDefaultProtocol ajpv12” specifies the protocol being used by Tomcat. “ApJServSecretKey DISABLED” disables the exchange of a secret key between Apache and Tomcat for security. This exchange has not been implemented. “ApJServMountCopy on” is used in case of virtual hosting. 

The line ApJServLogLevel notice specifies the amount of debugging information Tomcat will show. Apart from the default log level, notice, the other
log levels are emerg, alert, crit, error, warn, info
and debug. 

The next two lines ApJServDefaultHost localhost and ApJServDefaultPort 8007 specify the machine address and port on which Tomcat is running. The ApJServDefaultHost can be used if Apache Web server and Tomcat are running on different machines. 

The changes have been shown in magenta color. Here, we first we define an alias
/jsp for the directory /home/httpd/html/jsp. Next using the tag we instruct Apache to send all files with a JSP extension in the
/home/httpd/html/jsp directory to the jserv-servlet handler (that is, Tomcat). Finally we mount the
/jsp context as: 

ApJServMount /jsp

Mounting the context can be thought of to be similar to mounting filesystems in Linux. We do this so that the Tomcat module for Apache can be aware of the new context. Make sure to copy the file jspprogram.jsp in
/home/httpd/html/jsp. Now you must stop and restart Tomcat as well as Apache using the following commands:

/opt/tomcat/bin/shutdown.sh

/opt/tomcat/bin/startup.sh

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

Now you can run the jspprogram.jsp with the URL 

http://localhost/jsp/jspprogram.jsp

Shekhar Govindarajan

Stay connected with us through our social media channels for the latest updates and news!

Follow us: