Drop-Down Lists and Hyperlinks


This slide shows how to send the user input including a drop-down list to a server for processing. If you select some options below and click the “Submit” button, the browser will call the following scripts:
10.html ⇒ Select.cgi ⇒ Select.pl ⇒ Select.java ⇒ HTML ⇒ Hyperlink.pl
and list the descriptions hyperlinked with the corresponding cities based on the selected brands.


  <form method="post" action="../../../cgi-bin/520/6/Select.cgi"> 
    <select name="brand" multiple size="3">
     <option value="MKF Studios" /> MKF Studios
     <option value="Wolf" /> Wolf
     <option value="Parabuster Inc." /> Parabuster Inc.
     <option value="Big Studios" /> Big Studios
    </select>
    <input type="submit" name="act" value="Submit" />
    <input type="submit" name="act" value="HTML source" />
    <input type="submit" name="act" value="CGI source" />
    <input type="submit" name="act" value="Perl source" />
    <input type="submit" name="act" value="Java source" />
    <input type="submit" name="act" value="Hyperlink source" />
    <input type="submit" name="act" value="Help" />
    <input type="reset"             value="Reset" />
  </form>
         

     

The following code, Select.cgi is used to set the web environment for the Oracle database:

~/public_html/cgi-bin/520/6/Select.cgi
#!/usr/bin/bash

CLASSPATH=.:/usr/lib/oracle/12.1/client64
CLASSPATH=$CLASSPATH:/usr/lib/oracle/12.1/client64/lib/ojdbc7.jar
CLASSPATH=$CLASSPATH:/usr/lib/oracle/12.1/client64/lib/ottclasses.zip
export  CLASSPATH

/usr/bin/perl  Select.pl

The following Perl script is mainly used to process the web inputs including the drop-down list.

~/public_html/cgi-bin/520/6/Select.pl
#!/usr/bin/perl
use CGI;
$query = new CGI;
$act   = $query->param( 'act' );

if ( $act eq "Submit" ) {
  # Print HTML.
  print ( "Content-type: text/html\n\n" );

# Use "here-doc" syntax.
print <<EndofHTML;
  <html>
   <head>
    <link rel="stylesheet" type="text/css" href="http://undcemcs01.und.edu/~wen.chen.hu/css/1.css" />
   </head>
   <body text="#000000" vLink="#3366CC" link="#3366CC" bgColor="#ffffff"
     alink="#3366CC" background="http://undcemcs01.und.edu/~wen.chen.hu/bg/63.png">
    <center>
     <font size="+0" color="#3366CC">
EndofHTML

  # Compose a Java command.
  $cmd = "/usr/bin/java -Djava.security.egd=file:/dev/./urandom Select ";
  my @brands = $query->param( 'brand' );
  foreach my $brand (@brands) { $cmd .= "'" . $brand . "' "; }
  print( $cmd );      system( $cmd );

print <<EndofHTML;
     </b></font>
    </center>
   </body>
  </html>
EndofHTML
}
elsif ( $act eq "HTML source" ) {
  # Print plain text.
  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 ( "Content-type: text/plain\n\n" );
  system( "/bin/cat Select.cgi; echo \n\n\n\n" );
}
elsif ( $act eq "Perl source" ) {
  print ( "Content-type: text/plain\n\n" );
  system( "/bin/cat Select.pl; echo \n\n\n\n" );
}
elsif ( $act eq "Java source" ) {
  print ( "Content-type: text/plain\n\n" );
  system( "/bin/cat Select.txt; echo \n\n\n\n" );
}
elsif ( $act eq "Hyperlink source" ) {
  print ( "Content-type: text/plain\n\n" );
  system( "/bin/cat Hyperlink.pl; 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>" );
}

The following JDBC script accesses the database and retrieves the descriptions and cities.

~/public_html/cgi-bin/520/6/Select.java
/*******************************************************************

  This program shows how to list the cities and
    descriptions in the stores and productions tables
    based on the sales.

  To use this program, you need to create the following
    three tables by using the following commands:

  SQL> CREATE TABLE stores (
    2    store_key  INTEGER PRIMARY KEY,
    3    city       VARCHAR(32) NOT NULL,
    4    region     VARCHAR(16) NOT NULL );

  SQL> CREATE TABLE products (
    2    product_key  INTEGER PRIMARY KEY,
    3    description  VARCHAR(32) NOT NULL,
    4    brand        VARCHAR(32) NOT NULL );

  SQL> CREATE TABLE sales_fact (
    2    store_key    INTEGER,
    3    product_key  INTEGER,
    4    sales        NUMBER(5,2) NOT NULL,
    5    cost         NUMBER(5,2) NOT NULL,
    6    profit       NUMBER(5,2) NOT NULL,
    7    PRIMARY KEY ( store_key, product_key ),
    8    FOREIGN KEY ( store_key ) REFERENCES stores( store_key ) ON DELETE CASCADE,
    9    FOREIGN KEY ( product_key ) REFERENCES products( product_key ) ON DELETE CASCADE );

*******************************************************************/

// Import the following packages to use JDBC.
import  java.sql.*;
import  java.io.*;
import  oracle.jdbc.*;
import  oracle.jdbc.pool.OracleDataSource;

class  Select {
  public static void  main( String args[ ] ) throws SQLException {
    String user     = "C##userid";
    String password = "password";
    String database = "65.52.222.73:1521/cdb1";

    // Open an OracleDataSource and get a connection.
    OracleDataSource ods = new OracleDataSource( );
    ods.setURL     ( "jdbc:oracle:thin:@" + database );
    ods.setUser    ( user );
    ods.setPassword( password );
    Connection conn = ods.getConnection( );

    try {
      // Create, compose, and execute a statement.
      Statement stmt  = conn.createStatement( );
      String   query  = "select city, description from sales_fact f, stores s, ";
               query += "products p where (";
      if ( args.length == 0 ) query += "brand=' ')";
      else {
        for ( int i=0;  i < args.length;  i++ )
          if ( i == 0 ) query += "brand='"      + args[i].trim( );
          else          query += "' or brand='" + args[i].trim( );
        query += "') and f.product_key=p.product_key and f.store_key=s.store_key";
      }
      System.out.println( query + "<b>" );
      ResultSet rset = stmt.executeQuery( query );

      // Iterate through the result and print the data.
      while ( rset.next( ) ) {
        System.out.print( "<a href='http://undcemcs01.und.edu/~wen.chen.hu/cgi-bin/520/6/Hyperlink.pl?city=" );
        System.out.print( rset.getString(1) + "'>" + rset.getString(2) + "<a>" );
      }
      // Close the ResultSet and Statement.
      rset.close( );
      stmt.close( );
    }
    catch ( SQLException ex ) {
      System.out.println( ex );
    }
    // Close the Connection.
    conn.close( );
  }
}

The following Perl script is used to display the corresponding web page based on the city submitted by the GET method. The command
   print "Location: $url\n\n";
tells the browser to redirect to the URL at $url.

~/public_html/cgi-bin/520/6/Hyperlink.pl?city=
#!/usr/bin/perl
use CGI;
$query = new CGI;
$city  = $query->url_param( "city" );

if ( $city eq "New York" ) {
  $url = "http://www.nyc.gov/";
}
elsif ( $city eq "Chicago" ) {
  $url = "http://www.cityofchicago.org/";
}
elsif ( $city eq "Atlanta" ) {
  $url = "http://www.atlantaga.gov/";
}
elsif ( $city eq "Los Angles" ) {
  $url = "http://www.lacity.org/";
}
elsif ( $city eq "San Francisco" ) {
  $url = "https://en.wikipedia.org/wiki/San_Francisco";
}
elsif ( $city eq "Philadelphia" ) {
  $url = "http://www.phila.gov/";
}
else {
  $url = "http://www.grandforksgov.com/";
}
print "Location: $url\n\n";




      Women only call me ugly until they find out how much money I make.    
      Then they call me ugly and poor.