CGI with Tomcat 7

Software Requirements (used for this post):

  • Apache Tomcat 7.0.11
  • JDK 1.6 update 21
  • Perl 5.12 (ActivePerl-
  • Windows XP SP3
  1. Download Tomcat. Install or unzip it to some directory.
  2. Modify <TOMCAT_HOME>\conf\web.xml to uncomment the cgi servlet and its mapping
    and add an servlet parameter passShellEnvironment and set it to “true”
  3. Modify <TOMCAT_HOME>\conf\context.xml to add a property on <Context>:
    <Context privileged="true">
  4. Create a cgi folder under <TOMCAT_HOME>\webapps\ROOT\WEB-INF\
  5. Install Perl
  6. Create a CGI script and place it under <TOMCAT_HOME>\webapps\ROOT\WEB-INF\cgi\
    # filename: test1.cgi
    print "Content-type: text/html\n\n";
    print "Hello, world!\n"
  7. Start Tomcat and access the URL:


    CGI Script

This is a scheduled publication.
Disclaimer | Privacy Policy | About

About these ads


Filed under Configure Tomcat 7, Information Technology

27 responses to “CGI with Tomcat 7

  1. Nick

    write small script
    for run cmd command DIR.

    # print “Hello World.\n”;

    “Content-type: text/plain\n\n”;

    foreach $var (
    sort keys %ENV) {

    @values = split(
    foreach $i (@values) {
    ($varname, $mydata) = split(
    print “$varname = $mydata\n”;

    use CGI::Carp qw(fatalsToBrowser);
    print “Content-type: text/plain\n\n”;

    print “\n”;
    print “Run external program\n”;
    print “\n”;
    $X = `dir`;
    print “”;
    print $X;
    print “”;
    print “\n”;

    run URL http://localhost:8080/Monitor/cgi-bin/

    and have error

    HTTP Status 404 – Servlet cgi is not available


    type Status report

    message Servlet cgi is not available

    description The requested resource (Servlet cgi is not available) is not available.


    Apache Tomcat/7.0.11

  2. Hello!

    If you did exactly what’s on the blog, the URL you are using is wrong. It should be:


    From the URL you gave, it looks like your script belongs to a tomcat web application (Monitor). I am not sure if that is possible.

    On my blog, I was trying to use Tomcat as a “runtime engine” that had been largely modified to be bundled with some web applications and released as a stand-alone web application.

    I hope this helps.


  3. Dee

    Thank you for this… I’ve been trying to get perl to run in tomcat for a few weeks and needed the following:


  4. Hi Dee, you are very welcome! Thank you for dropping by. ;)

  5. a

    Perfect..!!! Simple and short.. :D

  6. Ajay Ranjan

    HI All,

    I am trying run perl script from Tomcat Version 7.
    I didnt exactly what said above. But still it seems tomcat is picking the script from TOMCAT_HOME/webapps/WEB-INF/cgi. and hence simply giving the perl script as text. No idea why it is not able to evoke perl.

    Please help.

  7. Hi Ajay,

    I saw your comment yesterday and approved it. However, I did not have time to look into it. My apologies. Duty calls. ;)

    About your comment, could you check if there was a typo error on your part? TOMCAT_HOME/webapps/WEB-INF/cgi looks like cgiPathPrefix was set to “/WEB-INF/cgi” and not “WEB-INF/cgi”.


  8. Richard Eyerly

    have you set this up on windows 2008? the passShellEnvironment set to true bit doesn’t seem to work on server 2008. works fine on 2003.

    • Hi Richard,

      It looks like a known issue before (I think TC6 or earlier) where, as far as I can remember, the value of is different to what Tomcat expects.

      What is the Tomcat’s and JDK’s versions you have?

      Could you tell me what the value is? Here is a simple Java program to display the value:

      public class TestOSName {
          public static void main(String args[]) {

      It should show up “Windows 2008″. If not, you may need to explicitly set on the command line as Tomcat is started. As in:”Windows 2008″

      I hope this helps and thank you for visiting my blog.


  9. Richard Eyerly

    I think my version of Tomcat is actually earlier than 7. I am using JBoss, and I don’t know how the versions match up. Adding the option to the command line fixed the issue, thanks!

  10. Chatu

    Hi, I followed the instructions exactly as it is, but I get HTTP Status 404 – Servlet cgi is not available

  11. HI.. I am using CGI with python
    After running the server, when I enter the desire link, instead of html content , whole python code is showing on the screen. Can you provide some help regarding this.

    • Hi Mohit,

      My apologies for the late. I started work with a new client this month and did not find time to reply sooner. In a nutshell, Tomcat invokes an interpreter and in most cases and/or by default it is perl. There must be a way to configure Tomcat to use Python instead of Perl. So instead “perl” command, “python” commad is used.


  12. bobdc

    I had been struggling trying to get CGIs running with Tomcat under Windows and your explanation worked perfectly the first time I tried it. So thanks!

  13. Great tutorial. Just enough detail to make it work, but not so much that it looks intimidating. Although I’ve used (and occasionally set up) webservers for years, I am a total newbie with Tomcat. The only additional thing required for my setup was to add this directive to the “cgi servlet” snippet:



  14. There is something left here. I was enabling cgi support on a Solaris installacion, Tomcat 7, I had to uncomment the following for it to work, which is not mentioned:


    Hope this helps someone else. Regards.

  15. Sorry, missed something it is:

  16. Well, I dont know, but the XML tags will not appear, the thing is I had to look for xml tags servlet-mapping for servlet-name cgi and uncomment that too. It doesnt appear in the tutorial.. Regards

  17. Hi Jaime,

    My apologies I was not able to reply sooner. I think you missed step 2. You need to uncomment the CGI servlet and its mapping in web.xml.


  18. Hi Karl,

    I have Tomcat 7.0.26 installed on one of the Linux boxes which has perl v5.8.8 installed on it.
    I followed the same steps except install perl as it was already done…but still the script did not run…getting “HTTP Status 404 – Servlet cgi is not available”. Can you please help ?

    url used – http://:8080/cgi/test.cgi


  19. Hi Neelesh,

    Please try http://servername:8080/cgi-bin/test.cgi.
    Your URL mapping for your CGI is /cgi-bin/*


    • Hi Carl,

      The URL – http://servername:8080/test/guestbook.html is working fine and the html content is rendered. After submit button “http://servername:8080/test/post.cgi” is invoked and it is returning – HTTP Status 404 – Servlet cgi is not available

      Please check below the details and help me fixing this. Your response is very appreciated. Thanks in Advance!
      contents of web.xml







      The path where the .cgi and .html file is kept :

      Your Name:
      Email Address:

      post.cgi :

      #!/usr/bin/perl -wT
      use CGI qw(:standard);
      use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
      use strict;

      print header;
      print start_html(“Thank You”);
      print h2(“Thank You”);

      my %form;
      foreach my $p (param()) {
      $form{$p} = param($p);
      print “$p = $form{$p}\n”;
      print end_html;

      Thanks & Regards,