Using Perl on Your Personal
Installation of Tomcat
CGI processing is handled in Tomcat with a servlet, CGIServlet, which supplied with the Tomcat installation. It is not enabled by default, however. You must install the JAR file containing the CGIServlet class file and set up use of the servlet in your web application descriptor file. This can be done for a particular web application or it for the entire Tomcat installation.
An apparent bug in CGIServlet prevents it from accessing Perl scripts in any directory whose path contains a space character. Since Tomcat is often installed in Windows under the Program Files directory, this can be a problem. If your Tomcat is installed in such a place, you should either
· Re-install Tomcat elsewhere
· Or set up a web application in a location in another location.
It is convenient to have your web applications under the webapps directory under the Tomcat installation directory because you do not have to configure them: just create a new subdirectory containing a WEB-INF/web.xml file and you’re ready to go. The URL path to access the new web application is simply the name of the directory you create. Therefore, I recommend installing Tomcat in a directory without spaces in its path to avoid the complications involved with setting up a web application in a non-default location.
The JAR file is already present in the correct directory, but it does not have the correct name. You need to look in the directory server/lib under the Tomcat installation directory, and rename the file whose name begins with servlets-cgi. The new name of the file should be servlets-cgi.jar.
To set up CGIServlet for the entire Tomcat installation, modify the default web application descriptor file, conf/web.xml, under the Tomcat installation directory. To set it up for a single web application, modify the file WEB-INF/web.xml under the web application’s root directory.
Your default web.xml file probably has the tags for CGIServlet already present, but commented out. You should remove the commenting and adjust parameters as needed.
Here is the servlet tag I use in my home installation. You can also find such a tag in your web.xml file on your Einstein account.
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF\cgi</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>6</param-value>
</init-param>
</servlet>
Explanation:
· The servlet-name tag defines a name that will be used later for mapping a URL pattern to the servlet.
· The servlet-class is found in the JAR file you renamed above.
· The init-param tags define parameters that the servlet will use:
o The parameter cgiPathPrefix tells where to look for your Perl scripts. It is a directory relative to the web application root.
o The parameter debug causes debug output to be logged, which I found useful in debugging my configuration. (It’s how I discovered the problem with spaces in the path name mentioned above.) The debug output appears in the web application log file. Normally, this is located in the logs directory under the Tomcat installation root. Look for a file containing the name of your web application.
Note: in your installation on Einstein, I have set up logging for your web application so it appears in a file under the logs directory under your web application root (webapp or webappw). You will probably have to make this directory accessible by “group” (mode 770) for logging to work.
Recall that you also need to set up URL mapping for the servlet. Here is the tag I use in my home installation:
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
Explanation:
· The servlet-name tag specifies the servlet name, which must match the name given in the servlet tag above.
· The url-pattern tag specifies the URLs that will map to this servlet. The above tag in a web application named “MyApp” would access the Perl script myperl.pl using the URL http://localhost:8080/MyApp/cgi-bin/myperl.pl (assuming you are accessing Tomcat locally and are using the default port, 8080.)
Download Perl from http://www.perl.org/
or other location of your choice. Make
sure that the directory containing the Perl executable appears in your system
environment variable %PATH% (for Windows).
You can check this using Start->Control Panel->System, for classic
style control panel. If you enable the
glitzy new style control panel, you’ll find System under Performance and
Maintenance on the main control panel window.
Click on the Advanced tab and use the
Environment Variables button at the bottom.
Using the servlet tags described above, you should install
your Perl scripts in the WEB-INF/cgi directory under your web application root
directory. They should have the .pl file
extension. I was unable to get CGIServlet
to use the first line of the file to determine which CGI command to use, so
this extension seems to be required.