Class cgi_lib

Class cgi_lib

java.lang.Object
   |
   +----cgi_lib

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.


Method Index

 o Environment()
Neatly format all of the CGI environment variables and the associated values using HTML.
 o Header()
Generate a standard HTTP HTML header.
 o HtmlBot()
Generate some vanilla HTML that you usually want to include at the bottom of any HTML page you generate.
 o HtmlTop(String)
Generate some vanilla HTML that you usually want to include at the top of any HTML page you generate.
 o main(String[])
The main routine is included here as a test CGI script to demonstrate the use of all of the methods provided above.
 o MethGet()
Determine if the REQUEST_METHOD used to send the data from the browser was the GET method.
 o MethPost()
Determine if the REQUEST_METHOD used to send the data from the browser was the POST method.
 o MyBaseURL()
Determine the Base URL of this script.
 o MyFullURL()
Determine the Full URL of this script.
 o ReadParse(InputStream)
Parse the form data passed from the browser into a Hashtable.
 o urlDecode(String)
URL decode a string.

Data passed through the CGI API is URL encoded by the browser.

 o Variables(Hashtable)
Neatly format all of the form data using HTML.

Methods

 o 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.
 o 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.
 o Header
  public static String Header()
Generate a standard HTTP HTML header.
Returns:
A String containing the standard HTTP HTML header.
 o HtmlTop
  public static String HtmlTop(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.
 o HtmlBot
  public static String HtmlBot()
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.
 o 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.
 o 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.
 o 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.
 o 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.
 o 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.
 o 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.
 o 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.HtmlTop());
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.