Forms and Input Text Fields
This slide shows how to use HTML form to send the user input from a single-line input text field to a server for processing.
If you type a region, Central, East, or West, in the text field below and click the “List” button, the browser will call the following scripts:
6.html ⇒ List.cgi ⇒ List.pl ⇒ List.java
and list the corresponding stores in that region.
An empty region will list all stores.
<form method="post" action="http://undcemcs01.und.edu/~wen.chen.hu/cgi-bin/jdbc/List.cgi">
Region:
<input type="text" name="region" />
<input type="submit" name="act" value="List" />
<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="Help" />
<input type="reset" value="Reset" />
</form>
|
|
The button “List” will activate the following code,
List.cgi
, which is used to set the web environment for the Oracle database:
~/public_html/cgi-bin/jdbc/List.cgi
|
#!/usr/bin/bash
CLASSPATH=.:/usr/lib/oracle/23/client64
CLASSPATH=$CLASSPATH:/usr/lib/oracle/23/client64/lib/ojdbc8.jar
CLASSPATH=$CLASSPATH:/usr/lib/oracle/23/client64/lib/ottclasses.zip
export CLASSPATH
/usr/bin/perl List.pl
|
The following Perl script is mainly used to process the web inputs.
It could be skipped by letting the shell and Java scripts handle the web inputs.
However, Perl is known for its strong string processing, which is greatly needed for web processing, so the Perl script is kept to do most part of string processing.
~/public_html/cgi-bin/jdbc/List.pl
|
#!/usr/bin/perl
use CGI;
$query = new CGI;
$act = $query->param( 'act' );
$region = $query->param( 'region' );
if ( $act eq "List" ) {
# 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 color="#3366CC">
EndofHTML
# Remove leading and trailing spacing.
$region =~ s/^\s*(\S*)\s*$/$1/;
# For security, remove some Unix metacharacters.
$region =~ s/;|>|>>|<|\*|\?|\&|\|//g;
# Compose a Java command.
$cmd = "/usr/bin/java -Djava.security.egd=file:/dev/./urandom List '";
$cmd .= $region . "'";
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 List.cgi; echo \n\n\n\n" );
}
elsif ( $act eq "Perl source" ) {
print ( "Content-type: text/plain\n\n" );
system( "/bin/cat List.pl; echo \n\n\n\n" );
}
elsif ( $act eq "Java source" ) {
print ( "Content-type: text/plain\n\n" );
system( "/bin/cat List.java; 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 stores.
~/public_html/cgi-bin/jdbc/List.java
|
/*******************************************************************
This program shows how to list the stores in the
stores table.
To use this program, you need to create a table
stores 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 );
Table created.
*******************************************************************/
// Import the following packages to use JDBC.
import java.sql.*;
import java.io.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class List {
public static void main( String args[ ] ) throws SQLException {
String user = "C##user_id";
String password = "password";
String database = "20.185.147.112:1521/xe";
// 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 store_key, city, region from stores ";
query += "where region like '%" + args[0].trim( ) + "%'";
System.out.println( query + "<b>" );
ResultSet rset = stmt.executeQuery( query );
// Iterate through the result and print the data.
while ( rset.next( ) ) {
System.out.print( rset.getString(1) + ", " + rset.getString(2) );
System.out.print( ", " + rset.getString(3) );
}
// Close the ResultSet and Statement.
rset.close( );
stmt.close( );
}
catch ( SQLException ex ) {
System.out.println( ex );
}
// Close the Connection.
conn.close( );
}
}
|