Running cgi-bin on Tomcat: Simple steps & gotchas
By Angsuman Chakraborty, Gaea News NetworkWednesday, 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.
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. I hope this helps. |
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 :
print "Content-type: text/html\n\n"; print "IN TEST!\n"; Store Location of test.cgi is : webapps/test/ Output in https://localhost:8080/test/test.cgi
|
trifonov |
April 25, 2007: 1:47 pm
Hi, I tried to add or param-name=executable 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 print “Content-type: text/html\n\n”; B) Changes to web.xml cgi executable clientInputTimeout debug cgiPathPrefix 5 cgi D) Renamed to servlets-cgi.jar E) Restarted server But when I do: |
December 15, 2005: 11:44 am
Try: 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. thanks |
December 2, 2005: 7:24 am
Hi Rock, Please refer to this article - It has been written to address your question. Best, |
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. |
alfredo |
May 6, 2005: 1:47 am
add |
alfredo |
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 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. |
Jim |
Samuel