sdsu.net
Class CGI

java.lang.Object
  |
  +--sdsu.net.CGI

public class CGI
extends java.lang.Object

This class is an interface to the Common Gateway Interface protocol used by most Unix based HTTP servers. It will take the values of an HTML form and make them available to the program through the getFormData() method. Supports data submitted via via http post, http put, http get and http isindex. Don't support all isindex queries. Support queries with path?name=value pairs, but not path?a+b+c

By itself, a java program cannot be used as a CGI program for two reasons:

  1. The java program is interpreted by the java interpeter so it cannot be executed as a single program without arguments.
  2. The CGI protocol uses environment variables to pass parameters from the HTTP server to the CGI program. Java doesn't have access to the environment variables.
For this reason, a companion program, jcgi, needs to be used.

The parsing of the CGI protocol stuff is done by the constructor of this class.

If this class is used in an interactive (testing) environment, requests for values will cause a interactive prompts. Once the value has been entered once, it is not asked for again. This is useful for debugging purposes.

Author:
Andrew Scherpbier, Roger Whitney made some modifications to Andrew's code

Method Summary
 java.lang.String decodeURL(java.lang.String URL)
          Convert an URL-escaped string to a non-escaped string.
 java.lang.String getBaseURL()
          Returns the Base URL of this script.
 java.util.Properties getCGIEnvironmentVariables()
          Returns a hashtable containing name-value pairs of the cgi environment variables.
 java.util.Properties getFormData()
          Returns a hashtable containing name-value pairs of the data sent via http post, http put, http get and http isindex.
 java.lang.String getFullURL()
          Returns the full URL of this script.
static CGI getInstance()
          Returns a CGI object
 boolean isGetRequest()
          Returns true if this is a get request
 boolean isPostRequest()
          Returns true if this is a post request
 boolean isPutRequest()
          Returns true if this is a put request
 void sendHtml(java.lang.String htmlMessage)
          Send an html encoded message to the client.
static void sendRedirect(java.lang.String url)
          Sends a url to client to use unstead of current url.
 void sendText(java.lang.String textMessage)
          Send a text message to the client.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static CGI getInstance()
Returns a CGI object

getFormData

public java.util.Properties getFormData()
Returns a hashtable containing name-value pairs of the data sent via http post, http put, http get and http isindex. All name and values are decoded into normal strings. If class is used in an interactive (testing) environment, the hashtable returned will prompt for the value associated a key the first time the key is accessed.

getCGIEnvironmentVariables

public java.util.Properties getCGIEnvironmentVariables()
Returns a hashtable containing name-value pairs of the cgi environment variables. The keys are the following strings:
AUTH_TYPEHTTP_ACCEPTREMOTE_ADDRSCRIPT_NAME
CONTENT_LENGTHHTTP_USER_AGENTREMOTE_HOSTSERVER_NAME
CONTENT_TYPEPATH_INFOREMOTE_IDENTSERVER_PORT
DOCUMENT_ROOTPATH_TRANSLATEDREMOTE_USERSERVER_PROTOCOL
GATEWAY_INTERFACEQUERY_STRINGREQUEST_METHODSERVER_SOFTWARE
See CGI description for details

getBaseURL

public java.lang.String getBaseURL()
Returns the Base URL of this script.

getFullURL

public java.lang.String getFullURL()
Returns the full URL of this script.

isPostRequest

public boolean isPostRequest()
Returns true if this is a post request

isGetRequest

public boolean isGetRequest()
Returns true if this is a get request

isPutRequest

public boolean isPutRequest()
Returns true if this is a put request

sendText

public void sendText(java.lang.String textMessage)
Send a text message to the client. The text will not be rendered as html by client. Useful for sending error messages to client. Can send repeated messages to client.

sendHtml

public void sendHtml(java.lang.String htmlMessage)
Send an html encoded message to the client. The message will be rendered as html by client. Can send repeated messages to client..

sendRedirect

public static void sendRedirect(java.lang.String url)
Sends a url to client to use unstead of current url. Url can be a full url or relative to current url. Does not make sense to send another message to client after calling this method, as client will drop connection upon getting this message.

decodeURL

public java.lang.String decodeURL(java.lang.String URL)
Convert an URL-escaped string to a non-escaped string. The special characters this method knows about are '+' which is converted to a space, and '%' which is followed by two hex digits that make up the character. The reverse action or converting a string into an URL-escaped string can be accomplished using the java.net.URLEncoder.encode() method.
Parameters:
URL - the escaped string
Returns:
the non-escaped string