HOW-TO Setup Windows XP and Tomcat for CGI using DOS Batch Files
by Sean aka Time Traveler
If you're someone who wants to dabble in how to setup some CGI in Tomcat and don't want to install a lot of other software, it's possible to do a fairly quick experiment this way usiing just Tomcat and some batch files.
This "FAQ" is really a tutorial on understanding directory relationships in Tomcat as far as CGI is concerned and mostly default settings.
Software Stack:
Assuming no Apache nor any type of perl is available (or desired)...
[ul]
[li]Windows XP Home (proabably for other versions of Windows, too)[/li]
[li]Tomcat 5.5 (maybe other versions of Tomcat)[/li]
[/ul]
Setup:
[ol]
[li]Install Tomcat from binary in .zip file[/li]
[li]Under Control Panel > System > Advanced > Environment Variables add the variable CATALINA_HOME and Tomcat's install path as a value[/li]
[li]If just using Tomcat and no webserver, edit port in %CATALINA_HOME%\conf\server.xml to port 80.[/li]
[li]In [tt]%CATALINA_HOME%\server\lib[/tt] rename [tt]servlets-cgi.renametojar[/tt] to [tt]servlets-cgi.jar[/tt][/li]
[li]In [tt]%CATALINA_HOME%\conf\web.xml[/tt] uncomment cgi servlet and cgi mapping block[/li]
[li]In [tt]%CATALINA_HOME%\conf\web.xml[/tt] in the cgi servlet block insert the parameter for [tt]executable[/tt] and set it to [tt]cmd /q /c[/tt][/li]
[li]
GOTCHA-ALERT SSI servlet and SSI mapping code blocks must also be uncommented to use any other [tt]webapps\*\WEB-INF\cgi\[/tt] directories, otherwise only [tt]webapps\ROOT\WEB-INF\cgi[/tt] will be active.[/li]
[li]In [tt]%CATALINA_HOME%\webapps[/tt] create following files and directory scheme:[/li]
[ul square]
[li]webapps[/li]
[ul square]
[li]ROOT\[/li]
[ul square]
[li]WEB-INF\[/li]
[ul square]
[li]cgi\[/li]
[ul square]
[li]foo.bat[/li]
[/ul]
[/ul]
[/ul]
[li]foo\[/li]
[ul square]
[li]index.jsp[/li]
[li]WEB-INF\[/li]
[ul square]
[li]web.xml[/li]
[/ul]
[/ul]
[/ul]
[/ul]
[li]Edit [tt]webapps\ROOT\WEB-INF\cgi\foo.bat[/tt], [tt]foo\index.jsp[/tt] and [tt]foo\WEB-INF\web.xml[/tt] above per code examples below[/li]
[li]Start Tomcat and load '/foo' web app per Tomcat instructions [/li]
[li]Load [tt]http://localhost/foo[/tt] into your browser and you should see the title 'Foo', plus current time and location of CGI directory.[/li]
[/ol]
Code Examples:
Code:
<!-- Filename %CATALINA_HOME%\conf\web.xml -->
...
<!-- uncomment the cgi servlet in ...\conf\web.xml -->
<servlet>
<servlet-name>cgi</servlet-name>
...
<!-- add this block here so it knows to use 'cmd' instead of 'perl' -->
<init-param>
<param-name>executable</param-name>
<param-value>cmd /q /c</param-value>
</init-param>
...
</servlet>
<!-- uncomment the cgi mapping block as well -->
Code:
<!-- filename: %CATALINA_HOME%\conf\server.xml -->
<!-- If not using a webserver, change line ~77 <Connector port"8080" to... -->
...
<Connector port "80"
...
Code:
<HTML>
<HEAD>
<!--
filename: %CATALINA_HOME%\webapps\foo\index.jsp
purpose: Test CGI batch file output on WinXP, Tomcat 5.5, no webserver
input: User clicks link(s) to test CGI
output: HTML generated by foo.bat
comments: Probably the ugliest HTML page, but useful...
-->
<% String title = "Foo JSP" %>
<%!
static final String[] foo = {
"/cgi-bin/foo.bat",
"./WEB-INF/cgi/foo.bat",
"foo.bat"
};
%>
<title><%= title %></title>
</HEAD>
<BODY>
<H1><%= title %></H1>
<hr>
<%
for (int bar = 0; bar < foo.length; bar++) {
String bat = foo[bar];
%>
<A HREF="<%= bat %>" TARGET="_self"><%= bat %></A>
<FORM METHOD="POST" ACTION="<%= bat %>"><INPUT TYPE="submit" VALUE="<%= bat %> POST"></FORM>
<HR>
<% } %>
<HR>
</BODY>
</HTML>
Code:
:: filename: %CATALINA_HOME%\foo\WEB-INF\cgi\foo.bat
:: purpose: Output HTML via CGI
:: input: None
:: output: Via HTML current time and directory on system
:: comments: No echo off needed if executable includes switch /q
:: Remember to escape special characters with a carot(^)
echo Content-type: text/html
echo.
echo.
echo ^<HTML^>
echo ^<BODY^>
echo ^<H1^>Foo^</H1^>
echo ^<P^>Time is:
time /t
echo ^</P^>
echo ^<P^>Location is:
chdir
echo ^</P^>
echo ^</BODY^>
echo ^</HTML^>
Code:
:: filename: %CATALINA_HOME%\foo\WEB-INF\cgi\drag_bat_here.bat
:: purpose: Test .bat file output before using in Tomcat
:: input: Drag foo.bat icon over this file's icon in Windows to test, user
:: must press any key for cmd window to terminate
:: output: What will go into the CGI servlet, but only to a cmd screen for viewing
cmd.exe /q /c "%1"
pause
Code:
<?xml versioin="1.0" encoding="ISO-8859-1"?>
<!--
filename: %CATALINA_HOME%\webapps\foo\WEB-INF\web.xml
purpose: Put a viewable title in http://localhost/manager/html
input: None
output: Per xml web-app usage
-->
<web-app>
<display-name>Foo</display-name>
<description>Welcome to Foo</description>
</web-app>
Good luck. -S