CGIRequest.java
Java Program to handle CGI requests
Most recent release 2007/Jun/20

The Common Gateway Interface (CGI) is a protocol allowing the completion of a form in a web page to trigger the execution of a program on a remote server to process the data from the form.

Java has efficient mechanisms for setting up such processes (Servlets and JSP) which require a special web server such as Tomcat to support them. If the transaction volume is fairly low, the overhead of starting up a new process for each transaction will not be a concern, and a normal web server can be used with the CGI program written in Java (or any other language).

To write a Java CGI program, it is necessary to provide a short script to set the program running. This is because Java programs are not directly executed but require a Java virtual machine to interpret them. Also, with older (pre-1.5) versions of Java, Java did not reliably support the use of environment variables for receiving information from the web server, which was the normal process. Instead, the environment variables had to be packaged up as system properties for a Java program to access them.

Pat L. Durante build a CGI program to assist in writing Java CGI programs, called cgi_lib.java. This works but suffers from a couple of design limitations. Firstly, forms containing checkbox groups that support multiple selection will not work. Secondly, if the volume of text coming from the web-page is large, it may be truncated by the CGI program.

The program CGIRequest.java is provided as an alternative to Pat's program. This will decode the text received from the web-page, and place the information into one or two Hashtables where it can be extracted by name. All that you need to do is provide a main program to create a CGIRequest instance, and then deal with the data in the Hashtables.

The June 2007 release adds the ability to receive Cookies from the remote site, and corrects a couple of minor bugs in handling special characters in a CGI request.

The March 2005 release added the ability to handle enctype="multipart/form-data" forms, ie. forms in which INPUT elements representing the contents of a file are supported.

You can download CGIRequest.java, or you can browse the specification of how to use it.


Mail to Graham Freeman


< < Back