Programming Exercise I 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 ListCourses.pl below is made minimal and performs the following three tasks:

    • The first part is to retrieve student’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;
         $student = $query->param( 'student' );
      the variable $student contains “Poke Mon” from the web input:
         <input type="text" name="student" value="Poke Mon">
      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/demo/10/ListCourses.pl
    #!/usr/bin/perl
    use CGI; 
    $query   = new CGI;
    $act     = $query->param('act');
    $student = $query->param('student');
    
    if ( $act eq "List courses" ) {
      # 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="+1"><b>
    EndofHTML
    
      # Remove leading and trailing spacing.
      $student =~ s/^\s*(\S*)\s*$/$1/;
      # For security, remove some Unix metacharacters.
      $student =~ s/;|>|>>|<|\*|\?|\&|\|//g;
    
      # Compose a Java command.
      $cmd = "/usr/bin/java -Djava.security.egd=file:/dev/./urandom ListCourses ";
      if ( ( $query->param('all') ) ||
           ( !$query->param('Poke Mon') && !$query->param('Sponge Bob') &&
             ( $student eq "" ) ) )  { $cmd .= "''"; }
      else {
        if ( $query->param('Poke Mon') )   { $cmd .= "'POKE MON' "; }
        if ( $query->param('Sponge Bob') ) { $cmd .= "'SPONGE BOB' "; }
        if ( !( $student eq "" ) )         { $cmd .= "'" . uc($student). "'"; }
      }
      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 ListCourses.cgi; echo \n\n\n\n" );
    }
    elsif ( $act eq "Perl source" ) {
      print ( "Content-type: text/plain\n\n" );
      system( "/bin/cat ListCourses.pl; echo \n\n\n\n" );
    }
    elsif ( $act eq "Java source" ) {
      print ( "Content-type: text/plain\n\n" );
      system( "/bin/cat ListCourses.txt; echo \n\n\n\n" );
    }
    elsif ( $act eq "SQL source" ) {
      print ( "Content-type: text/plain\n\n" );
      system( "/bin/cat ListCourses.sql; echo \n\n\n\n" );
    }
    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>" );
    }



      A picture paints a thousand words.