Running cgi-bin on Tomcat: Simple steps & gotchas

By Angsuman Chakraborty, Gaea News Network
Wednesday, March 24, 2004

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 <web-app> and </web> tags.

<servlet>
<servlet-name>cgi</servlet>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet>
<init -param>
<param-name>clientInputTimeout</param>
<param-value>100</param>
</init>
<init-param>
<param-name>debug</param>
<param-value>6</param>
</init>
<init-param>
<param-name>cgiPathPrefix</param>
<param-value>WEB-INF/cgi</param>
</init>
<load-on-startup>5</load>
</servlet>

<servlet -mapping>
<servlet-name>cgi</servlet>
<url-pattern>/cgi-bin/*</url>
</servlet>

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.

Discussion

Samuel
November 11, 2009: 8:13 am

There is a mising “pre” tag around the XML code included, so only the litteral text elements show up on this page.
Use “View page source” to access the real code to use.
Or fetch it from e.g. this page: wellho.net/solutions/java-running-cgi-scripts-in-apache-tomcat.html

I hope this helps.
//Samuel


hairstyles
October 21, 2009: 11:07 am

Whatever be the bugs, Windows Vista reigned supreme in one category and that was its looks. Anything that looks good sells


Vivek
October 19, 2007: 3:48 am

Please help me to fix the working of CGI which I mentioned above. I am not able to execute ang CGI through TOMCAT.


Vivek
October 19, 2007: 3:37 am

test.cgi is :


#!\perl\bin\perl.exe

print "Content-type: text/html\n\n";

print "IN TEST!\n";
print "\n";
print "IN TEST!\n";
print "\n";

Store Location of test.cgi is : webapps/test/

Output in https://localhost:8080/test/test.cgi


#!\perl\bin\perl.exe print "Content-type: text/html\n\n"; print "\n"; print "\n"; print "
IN TEST!
\n"; print "\n";


trifonov
April 25, 2007: 1:47 pm

Hi,
I followed the steps but I cannot start bat file as cgi script on WinXP. The error is: “java.io.IOException: Cannot run program “perl” (in directory “C:\Program Files\Tomcat 5.5\webapps\test\WEB-INF\cgi”)

I tried to add
param-name=executable
value=cmd /c

or param-name=executable
value=cmd

but without success. Any ideas?

April 28, 2006: 12:24 pm

The steps have worked perfectly for many including myself.

Unfortunately I don’t have time at this moment to support your specific issue.


Nitin Uchil
April 20, 2006: 12:42 pm

I have been trying to put perl scripts in webapps/ROOT/WEB-INF/cgi-bin:

A) printEnv.cgi
#!/usr/bin/perl

print “Content-type: text/html\n\n”;
print “\n”;
foreach $key (sort keys(%ENV)) {
print “$key = $ENV{$key}”;
}

B) Changes to web.xml

cgi
org.apache.catalina.servlets.CGIServlet

executable
/usr/bin/perl

clientInputTimeout
100

debug
6

cgiPathPrefix
WEB-INF/cgi-bin

5

cgi
/cgi-bin/*

D) Renamed to servlets-cgi.jar

E) Restarted server

But when I do:
https://mydomain/cgi-bin/printEnv.cgi
it does not go to the URL

December 15, 2005: 11:44 am

Try:
https://localhost:8080/Your web application directory]/cgi-bin/foo.sh

Please closely follow the instructions above.


Breno Leitao
December 15, 2005: 10:14 am

Please, what is the URL that I shoul enter in order to get the cgi executed?

Does the url below shouldn’t work? it isn’t.
http:localhost:8080//cgi-bin/foo.sh

thanks
Breno Leitão

December 2, 2005: 7:24 am

Hi Rock,

Please refer to this article -
How to Send / Receive Emails (SMTP & POP) From Localhost; Simple Freeware MailServer

It has been written to address your question.

Best,
Angsuman


rock
December 2, 2005: 4:55 am

hi all.can anybody help me to send and receive mail from my localhost.im using tomcat5 as my localserver.plz help me out..thanks to all


anurag
May 6, 2005: 1:49 am

Directly add executables including batch files.
However if your want to execute cmd commands like dir then you will have to use cmd /c dir.


alfredo
May 6, 2005: 1:47 am

add
param-name=executable
value=
or (windows xp)
param-name=executable
value=cmd /c


alfredo
May 6, 2005: 1:43 am

Add

executable

or (windows xp)

executable
cmd /c

April 4, 2005: 3:14 pm

@Giles Specifying full-path looks logical to me. Java doesn’t invoke executables by looking at the path variable.


Giles Lewis
March 25, 2005: 1:58 pm

I had to specify an executable init param equal to the full path of perl in order to get this to work.

executable
full path of perl

I am not sure why I had to do this since perl should already be in the path. I’m running TomCat 5 on Windows XP Professional.

December 11, 2004: 2:11 pm

I did at the bottom.


Jim
April 23, 2004: 1:07 pm

Don’t forget to uncomment the servlet-mapping for cgi

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :