Monday, January 30, 2006

Enabling JSP in Tomcat

Sometimes you just want to enable a directory on your tomcat server to be able to run JSP pages without actually installing a WAR or EAR file. Here are the essential steps to enable a directory to execute JSP files.

For this example, I'm setting it up on a SuSE Linux system so your directory structure may vary.


  1. Create a JSP directory in your tomcat webapps directory. In this case, the default webapps directory for Tomcat is /srv/www/tomcat5/base/webapps/ so I make a jsp directory there.

  2. Under the jsp directory I create the basic webapp directory structure and create a directory named /srv/www/tomcat5/base/webapps/jsp/WEB-INF

  3. In the WEB-INF directory, I create a basic deployment descriptor named web.xml which looks like:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
    <web-app>
    <display-name>My JSPs<display-name>
    </web-app>


  4. In the tomcat configuration directory /etc/tomcat5/base/Catalina/localhost add the context path for the JSPs in a file named jsp.xml that looks like:

    <Context path="/jsp" docBase="jsp"
    debug="0" reloadable="true" />


  5. Restart Tomcat (rctomcat5 restart)

  6. Create some sample JSP file in the jsp directory (/srv/www/tomcat5/base/webapps/jsp such as:

    <html>
    <head>
    <title>Hello World</title>
    </head>
    <body>
    <h1><%= "Hello World" %><h1>
    </body>
    </html>


  7. Make sure it's working via http://localhost:8080/jsp/index.jsp

  8. I run Tomcat under Apache via mod_jk so that I can utilize the SSL layer of Apache. So, to make the JSP transparently accessible from Apache, I update the /etc/apache2/conf.d/jk.conf and add the following lines:

    # Add the jsp directory
    Alias /jsp "/srv/www/tomcat5/base/webapps/jsp"
    <Directory "/srv/www/tomcat5/base/webapps/jsp">
    Options Indexes FollowSymLinks
    allow from all
    </Directory>
    JkMount /jsp ajp13
    JkMount /jsp/* ajp13
    <Location "/jsp/WEB-INF/">
    AllowOverride None
    deny from all
    </Location>


  9. After restarting Apache (rcapache2 restart) I can then reference the JSPs directly from Apache, such as http://localhost/jsp/index.jsp

No comments: