CGI with Tomcat 7


Software Requirements (used for this post):

  • Apache Tomcat 7.0.11
  • JDK 1.6 update 21
  • Perl 5.12 (ActivePerl-5.12.2.1202)
  • 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”
    <servlet>
     <servlet-name>cgi</servlet-name>
     <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
     <init-param>
     <param-name>debug</param-name>
     <param-value>0</param-value>
     </init-param>
     <init-param>
     <param-name>cgiPathPrefix</param-name>
     <param-value>WEB-INF/cgi</param-value>
     </init-param>
     <init-param>
     <param-name>passShellEnvironment</param-name>
     <param-value>true</param-value>
     </init-param>
     <load-on-startup>5</load-on-startup>
    </servlet>
    
    <servlet-mapping>
     <servlet-name>cgi</servlet-name>
     <url-pattern>/cgi-bin/*</url-pattern>
    </servlet-mapping>
    
  3. Modify <TOMCAT_HOME>\conf\context.xml to add a property on <Context>:
    <Context privileged="true">
    ...
    </Context>
    
  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\
    #!C:\Perl\bin\perl.exe
    # filename: test1.cgi
    print "Content-type: text/html\n\n";
    print "Hello, world!\n"
    
  7. Start Tomcat and access the URL:

    http://localhost:8080/cgi-bin/test1.cgi

    CGI Script


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

About these ads

About karlsangabriel

State-of-the-bad-ass-art
This entry was posted in Configure Tomcat 7, Information Technology and tagged , , , . Bookmark the permalink.

27 Responses to CGI with Tomcat 7

  1. Nick says:

    write small script
    for run cmd command DIR.

    #!c:\perl\bin\perl.exe
    # print “Hello World.\n”;

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

    foreach $var (
    sort keys %ENV) {
    print
    “$var=\”$ENV{$var}\”\n”;
    }

    @values = split(
    /&/,$ENV{‘QUERY_STRING’});
    foreach $i (@values) {
    ($varname, $mydata) = split(
    /=/,$i);
    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/Run_CMD_Dir.pl

    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:

    http://localhost:8080/cgi-bin/Run_CMD_Dir.pl

    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.

    Regards,
    Karl

  3. Dee says:

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

    passShellEnvironment

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

  5. a says:

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

  6. Ajay Ranjan says:

    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”.

    Regards,
    Karl

  8. Richard Eyerly says:

    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 os.name is different to what Tomcat expects.

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

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

      public class TestOSName {
          public static void main(String args[]) {
              System.out.println(System.getProperty("os.name"));
          }
      }
      

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

      -Dos.name=”Windows 2008″

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

      Regards,
      Karl

  9. Richard Eyerly says:

    Hi,
    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 says:

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

  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.

      Thanks,
      Karl

  12. bobdc says:

    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. Mark Eden says:

    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:

    executable
    C:\strawberry\perl\bin\perl.exe

    -Mark

  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:

    cgi
    /cgi-bin/*

    Hope this helps someone else. Regards.

  15. Sorry, missed something it is:
    \
    \cgi\
    \/cgi-bin/*\
    \

  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.

    Thanks,
    Karl

  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

    Thanks!
    neeleshrpatil@gmail.com

  19. Hi Neelesh,

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

    Cheers,
    Karl

    • 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

      cgi
      org.apache.catalina.servlets.CGIServlet

      debug
      0

      cgiPathPrefix
      WEB-INF/cgi

      passShellEnvironment
      true

      5

      cgi
      *.cgi

      ___________________________________________________________
      The path where the .cgi and .html file is kept :
      /Tomcat/apache-tomcat-7.0.26/webapps/test
      ___________________________________________________________
      guestbook.html:

      Your Name:
      Email Address:
      Comments:

      ___________________________________________________________
      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,
      Neelesh

      • My apologies for late reply. I doubt you followed exactly the steps herein. If you did, you would notice that the .cgi and .class files should not be in the same directory.

What's your thoughts on the matter?

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s