Sie sind hier

Using Java for CGI Applications

Gespeichert von Marc Balmer am Mo., 01/01/1996 - 02:00

Copyright © 1996 by Marc Balmer, CH-4055 Basel, Switzerland. This paper discusses strategies and methods to use Java as a programming language for CGI (Common Gateway Interface) applications.

Problems with Java when used for CGI programs

Any programming language can theoretically be used for CGI-applications. With java there are, however, some problems that need to be resolved:

  1. On most systems, java binaries can not be called from the command line just by entering its name. In most cases it has to be started somehow like

    java -jit my_application

  2. Web-Servers place CGI related information in the environment, so the CGI-application can read those values using a getenv() call. Unfortunately, a java application has no access to the environment.


Solving the startup problem

To solve the java-startup problem, a wrapper has been written by the author to provide direct startup of java binaries. The wrapper is used by first creating a link with the same name as the .class file, but without the ".class". If the class file is "Hello.class" the link will be named "Hello". When the wrapper is started it will determine the name under which it was called and run the appropriate java class file using the java interpreter. If a class file can not be loaded the wrapper will output an error message to the client using HTML.

Solving the environment problem

As mentioned above, a java application has no access to the environment variables. The web-server places CGI related information in environment variables like "REMOTE_HOST" so these variables need to be transferred somehow to the java application. Fortunately the java interpreter allows the definition of system properties using the "-D" option on its command line. Thus to define REMOTE_HOST as, you call the java interpreter somehow like

java -jit my_application

The wrapper will place the CGI-related environment variables on the command line in the way just described.


Calling Java Programs as CGI-Applications

The Java-Wrapper

The java wrapper is a shell script:

# Java-Wrapper Version 1.1
# Copyright (C) 1996 by Marc Balmer, CH-4055 Basel,
# This wrapper executes java binaries as CGI programs

JAVA="/usr/java/bin/java -jit"

if [ -f $CLASS.class ] ; then

                $JAVA \
               -DREMOTE_HOST=$REMOTE_HOST \
               -DREMOTE_USER=$REMOTE_USER \
               -DHTTP_USER_AGENT=`echo $HTTP_USER_AGENT | $TR " " "+"` \
               -DHTTP_HOST=$HTTP_HOST \
               -DSERVER_NAME=$SERVER_NAME \
               -DSCRIPT_NAME=$SCRIPT_NAME \
               -DSERVER_PORT=$SERVER_PORT \
               -DREMOTE_ADDR=$REMOTE_ADDR \
       echo Content-type: text/html
       echo \<head\>\<title\>Java-Wrapper Error\</title\>\</head\>
       echo \<body\>
       echo \<h1\>Java-Wrapper error\</h1\>
       echo The requested java class file \<b\>$CLASS.class\</b\>
       echo was not found on this server.
       echo \</body\>

There is an odd-looking statement for the CGI-environment variable HTTP_USER_AGENT. It is necessary to use the tr command to convert all spaces to `+' characters as the java property loader class doesn't like spaces to much.

Related Documents

A complete application framework for CGI applications written in Java is discussed in a separate document. See The CGI-Application Framework for Java for an in-depth discussion.