Programming Exercise IV Construction (Cont.)

  1. Calling a CGI (Common Gateway Interface) Script (Perl)
  2. JDBC instead of Perl programming is the focus of this demonstration, but Perl is popular for web programming. Therefore, the CGI Perl script ListRoutes.pl below is made minimal and performs the following three tasks:

    • The first part is to retrieve user’s data from the Web and put it into variables by using the param function. For example, after executing the following commands:
       use CGI; 
       $query = new CGI;
       $stops = $query->param( 'stops' );
      the variable $stops contains “UND School” from the web input:
       <input type="text" name="stops" value="UND School">
      For details, check Perl CGI web-related module.

    • It then processes the HTML elements such as checkboxes and textarea. For details, check CGI Programming 101.

    • The third task does the actual processing by calling the programs of embedded-SQL languages such as JDBC or 3G languages such as C.

    ~/public_html/cgi-bin/280/4/ListRoutes.pl
    #!/usr/bin/perl
    use CGI; 
    $query = new CGI;
    $act   = $query->param( 'act' );
    $stops = $query->param( 'stops' );
    
    if ( $act eq "Find routes" ) {
      # Print HTML.
      print( "Content-type: text/html\n\n" );
    
    # Use "here-doc" syntax.
    print <<EndofHTML;
      <html>
       <body>
        <table width="100%" height="80%">
         <tr>
          <td align="center">
           <font size="+0"><b>
    EndofHTML
    
      # Remove leading and trailing spacing.
      $stops =~ s/^\s*(\S*)\s*$/$1/;
      # For security, remove some Unix metacharacters.
      $stops =~ s/;|>|>>|<|\*|\?|\&|\|//g;
      # Compose a Java command.
      $cmd   =  "/usr/bin/java -Djava.security.egd=file:/dev/./urandom ListRoutes " . $stops;
      print( $cmd );    system( $cmd );
    
    print <<EndofHTML;
            <form> 
             <input type="button" value=" Back " onclick="history.go(-1);return false;" />
            </form>
           </b></font>
          </td>
         </tr>
        </table>
       </body>
      </html>
    EndofHTML
    }
    elsif ( $act eq "HTML source" ) {
      print ( "Content-type: text/plain\n\n" );
      $cmd  = "/usr/bin/lynx -dump -source " . $ENV{HTTP_REFERER}; 
      $cmd .= "; echo \n\n\n\n";
      system( $cmd );
    }
    elsif ( $act eq "CGI source" ) {
      # Print plain text.
      print ( "Content-type: text/plain\n\n" );
      system( "/bin/cat ListRoutes.cgi; echo \n\n\n\n" );
    }
    elsif ( $act eq "Perl source" ) {
      print ( "Content-type: text/plain\n\n" );
      system( "/bin/cat ListRoutes.pl; echo \n\n\n\n" );
    }
    elsif ( $act eq "Java source" ) {
      print ( "Content-type: text/plain\n\n" );
      system( "/bin/cat ListRoutes.java; echo \n\n\n\n" );
    }
    elsif ( $act eq "SQL source" ) {
      print ( "Content-type: text/plain\n\n\n" );
      print ( "SELECT DISTINCT rs.rid FROM route_stop rs, stop s WHERE" );
      print ( "  ( UPPER(s.name) LIKE '%UND%' OR UPPER(s.name) LIKE" );
      print ( "  '%SCHOOL%' ) AND s.sid = rs.sid ORDER BY rs.rid" );
    }
    elsif ( $act eq "Help" ) {
      print ( "Content-type: text/html\n\n" );
      system( "/bin/cat  Help.html" );
    }
    else {
      print( "Content-type: text/html\n\n" );
      print( "No such option: <em>$act</em>" );
    }



      He says he’s going to start a business selling bees as pets.    
      I think he may be a few sandwiches short of a picnic.