×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

General e-Business discussion FAQ

Tomcat, CGI, and no web server

Example of using Tomcat in Windows for CGI without web server by TimeTraveler
Posted: 17 Nov 06 (Edited 20 Nov 06)

HOW-TO Setup Windows XP and Tomcat for CGI using DOS Batch Files
by Sean aka Time Traveler rockband

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)...

  • Windows XP Home (proabably for other versions of Windows, too)
  • Tomcat 5.5 (maybe other versions of Tomcat)
Setup:

  1. Install Tomcat from binary in .zip file
  2. Under Control Panel > System > Advanced > Environment Variables add the variable CATALINA_HOME and Tomcat's install path as a value
  3. If just using Tomcat and no webserver, edit port in %CATALINA_HOME%\conf\server.xml to port 80.
  4. In %CATALINA_HOME%\server\lib rename servlets-cgi.renametojar to servlets-cgi.jar
  5. In %CATALINA_HOME%\conf\web.xml uncomment cgi servlet and cgi mapping block
  6. In %CATALINA_HOME%\conf\web.xml in the cgi servlet block insert the parameter for executable and set it to cmd /q /c
  7. GOTCHA-ALERT SSI servlet and SSI mapping code blocks must also be uncommented to use any other webapps\*\WEB-INF\cgi\ directories, otherwise only webapps\ROOT\WEB-INF\cgi will be active.
  8. In %CATALINA_HOME%\webapps create following files and directory scheme:
    • webapps
      • ROOT\
        • WEB-INF\
          • cgi\
            • foo.bat
             
      • foo\
        • index.jsp
        • WEB-INF\
          • web.xml
          
  9. Edit webapps\ROOT\WEB-INF\cgi\foo.bat, foo\index.jsp and foo\WEB-INF\web.xml above per code examples below
  10. Start Tomcat and load '/foo' web app per Tomcat instructions
  11. Load http://localhost/foo into your browser and you should see the title 'Foo', plus current time and location of CGI directory.
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

Back to General e-Business discussion FAQ Index
Back to General e-Business discussion Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close