Running cgi-bin on Tomcat: Simple steps & gotchas
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
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.
Filed under Headline News, How To, Java Software |
|
RSS 2.0 |
Trackback this Article
|
Email this Article
You may also like to read |




































April 23rd, 2004 at 1:07 pm
Don’t forget to uncomment the servlet-mapping for cgi
December 11th, 2004 at 2:11 pm
I did at the bottom.
March 25th, 2005 at 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.
April 4th, 2005 at 3:14 pm
@Giles Specifying full-path looks logical to me. Java doesn’t invoke executables by looking at the path variable.
May 6th, 2005 at 1:43 am
Add
executable
or (windows xp)
executable
cmd /c
May 6th, 2005 at 1:47 am
add
param-name=executable
value=
or (windows xp)
param-name=executable
value=cmd /c
May 6th, 2005 at 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.
October 4th, 2005 at 5:04 am
December 2nd, 2005 at 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
December 2nd, 2005 at 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
December 15th, 2005 at 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 15th, 2005 at 11:44 am
Try:
http://localhost:8080/Your web application directory]/cgi-bin/foo.sh
Please closely follow the instructions above.
April 20th, 2006 at 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:
http://mydomain/cgi-bin/printEnv.cgi
it does not go to the URL
April 28th, 2006 at 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.
April 25th, 2007 at 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?
October 19th, 2007 at 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 http://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";
October 19th, 2007 at 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.