cgi-bin programs (common executables) can be run from Tomcat. It is however not enabled by default.
To enable it carefully follow the steps below:
1. Uncomment (or add) the following sections to $CATALINA_HOME/conf/web.xml (%CATALINA_HOME%\conf\web.xml on windows), between the and tags.


cgi

org.apache.catalina.servlets.CGIServlet

clientInputTimeout
100


debug
6


cgiPathPrefix
WEB-INF/cgi

5


cgi

/cgi-bin/*

2. Rename the $CATALINA_HOME/server/lib/servlets-cgi.renametojar (%CATALINA_HOME%\server\lib\servlets-cgi.renametojar on windows) file to $CATALINA_HOME/server/lib/servlets-cgi.jar (%CATALINA_HOME%\server\lib\servlets-cgi.jar on windows)

3. Restart Tomcat

Testing with Hello.bat
A simple way to test would be to create a cgi directory under WEB-INF directory of your Web application.

Lets add a simple file called hello.bat (for windows users).
The file contains just the line:
echo Hello World!

This displays the String "Hello World!"

Create a file called env.bat
The file contains just the line:
set

This displays all the available environment variables for the cgi-bin program.

Gotchas on windows
——————
Normally we use @echo off in a batch file to turn off displaying the command. However when we do the following, no output is displayed:

@echo off
echo Hello World!

It just needs a newline to be happy. So create a file (say empty.txt) which just contains a newline. Then change hello.bat to the following:
@echo off
type empty.txt
echo Hello World!
echo on

This works as intended.

Note: Would you like to show a token of appreciation for my hard work? I enjoy a cup of Cafe Estima Blend from Starbucks.