Open SiteSearch 4.1.1
Final

ORG.oclc.ecat.util
Class cgi_lib

java.lang.Object
  |
  +--ORG.oclc.ecat.util.cgi_lib

public class cgi_lib
extends Object

cgi_lib.java

Usage: This library of java functions, which I have encapsulated inside a class called cgi_lib as class (static) member functions, attempts to duplicate the standard PERL CGI library (cgi-lib.pl). You must invoke any Java program that uses this library from within a UNIX script, Windows batch file or equivalent. As you will see in the following example, all of the CGI environment variables must be passed from the script into the Java application using the -D option of the Java interpreter. This example UNIX script uses the "main" routine of this class as a CGI script:

 (testcgi.sh)

 #!/bin/sh

 java \
  -Dcgi.content_type=$CONTENT_TYPE \
  -Dcgi.content_length=$CONTENT_LENGTH \
  -Dcgi.request_method=$REQUEST_METHOD \
  -Dcgi.query_string=$QUERY_STRING \
  -Dcgi.server_name=$SERVER_NAME \
  -Dcgi.server_port=$SERVER_PORT \
  -Dcgi.script_name=$SCRIPT_NAME \
  -Dcgi.path_info=$PATH_INFO \
 cgi_lib

 
Question and comments can be sent to pldurante@tasc.com.


Constructor Summary
cgi_lib()
           
 
Method Summary
static String Environment()
          Neatly format all of the CGI environment variables and the associated values using HTML.
static String Header()
          Generate a standard HTTP HTML header.
static String include(String file)
          Load the contents of a file into a string.
static void main(String[] args)
          The main routine is included here as a test CGI script to demonstrate the use of all of the methods provided above.
static boolean MethGet()
          Determine if the REQUEST_METHOD used to send the data from the browser was the GET method.
static boolean MethPost()
          Determine if the REQUEST_METHOD used to send the data from the browser was the POST method.
static String MyBaseURL()
          Determine the Base URL of this script.
static String MyFullURL()
          Determine the Full URL of this script.
static String printHead(String Title)
          Generate some vanilla HTML that you usually want to include at the top of any HTML page you generate.
static String printTail()
          Generate some vanilla HTML that you usually want to include at the bottom of any HTML page you generate.
static Hashtable ReadParse(InputStream inStream)
          Parse the form data passed from the browser into a Hashtable.
static String urlDecode(String in)
          URL decode a string. Data passed through the CGI API is URL encoded by the browser.
static String Variables(Hashtable form_data)
          Neatly format all of the form data using HTML.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

cgi_lib

public cgi_lib()
Method Detail

ReadParse

public static Hashtable ReadParse(InputStream inStream)
Parse the form data passed from the browser into a Hashtable. The names of the input fields on the HTML form will be used as the keys to the Hashtable returned. If you have a form that contains an input field such as this,

   <INPUT SIZE=40 TYPE="text" NAME="email" VALUE="pldurante@tasc.com">
 
then after calling this method like this,

    Hashtable form_data = cgi_lib.ReadParse(System.in);
 
you can access that email field as follows:

    String email_addr = (String)form_data.get("email");
 
Parameters:
inStream - The input stream from which the form data can be read. (Only used if the form data was posted using the POST method. Usually, you will want to simply pass in System.in for this parameter.)
Returns:
The form data is parsed and returned in a Hashtable in which the keys represent the names of the input fields.

urlDecode

public static String urlDecode(String in)
URL decode a string.

Data passed through the CGI API is URL encoded by the browser. All spaces are turned into plus characters (+) and all "special" characters are hex escaped into a %dd format (where dd is the hex ASCII value that represents the original character). You probably won't ever need to call this routine directly; it is used by the ReadParse method to decode the form data.

Parameters:
in - The string you wish to decode.
Returns:
The decoded string.

Header

public static String Header()
Generate a standard HTTP HTML header.
Returns:
A String containing the standard HTTP HTML header.

printHead

public static String printHead(String Title)
Generate some vanilla HTML that you usually want to include at the top of any HTML page you generate.
Parameters:
Title - The title you want to put on the page.
Returns:
A String containing the top portion of an HTML file.

printTail

public static String printTail()
Generate some vanilla HTML that you usually want to include at the bottom of any HTML page you generate.
Returns:
A String containing the bottom portion of an HTML file.

MethGet

public static boolean MethGet()
Determine if the REQUEST_METHOD used to send the data from the browser was the GET method.
Returns:
true, if the REQUEST_METHOD was GET. false, otherwise.

MethPost

public static boolean MethPost()
Determine if the REQUEST_METHOD used to send the data from the browser was the POST method.
Returns:
true, if the REQUEST_METHOD was POST. false, otherwise.

MyBaseURL

public static String MyBaseURL()
Determine the Base URL of this script. (Does not include the QUERY_STRING (if any) or PATH_INFO (if any).
Returns:
The Base URL of this script as a String.

MyFullURL

public static String MyFullURL()
Determine the Full URL of this script. (Includes the QUERY_STRING (if any) or PATH_INFO (if any).
Returns:
The Full URL of this script as a String.

Environment

public static String Environment()
Neatly format all of the CGI environment variables and the associated values using HTML.
Returns:
A String containing an HTML representation of the CGI environment variables and the associated values.

Variables

public static String Variables(Hashtable form_data)
Neatly format all of the form data using HTML.
Parameters:
form_data - The Hashtable containing the form data which was parsed using the ReadParse method.
Returns:
A String containing an HTML representation of all of the form variables and the associated values.

include

public static String include(String file)
Load the contents of a file into a string.
Parameters:
file - The file to be loaded.
Returns:
A String representation of the file.

main

public static void main(String[] args)
The main routine is included here as a test CGI script to demonstrate the use of all of the methods provided above. You can use it to test your ability to execute a CGI script written in Java. See the sample UNIX script file included above to see how you would invoke this routine.

Please note that this routine references the member functions directly (since they are in the same class), but you would have to reference the member functions using the class name prefix to use them in your own CGI application:

     System.out.println(cgi_lib.printHead());
 
Parameters:
args - An array of Strings containing any command line parameters supplied when this program in invoked. Any command line parameters supplied are ignored by this routine.

Open SiteSearch 4.1.1
Final