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:
- 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
- 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 odin.msys.ch, you call the java interpreter somehow like
java -jit -DREMOTE_HOST=odin.msys.ch 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 is a shell script:
#!/bin/sh # Java-Wrapper Version 1.1 # Copyright (C) 1996 by Marc Balmer, CH-4055 Basel, firstname.lastname@example.org. # This wrapper executes java binaries as CGI programs BASENAME=/sbin/basename JAVA="/usr/java/bin/java -jit" TR=/usr/bin/tr CLASS=`$BASENAME $0` 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 \ -DGATEWAY_INTERFACE=$GATEWAY_INTERFACE \ -DHTTP_CONNECTION=$HTTP_CONNECTION \ -DSERVER_SOFTWARE=$SERVER_SOFTWARE \ -DREQUEST_METHOD=$REQUEST_METHOD \ -DSERVER_NAME=$SERVER_NAME \ -DSCRIPT_NAME=$SCRIPT_NAME \ -DSERVER_PORT=$SERVER_PORT \ -DSERVER_PROTOCOL=$SERVER_PROTOCOL \ -DREMOTE_ADDR=$REMOTE_ADDR \ -DDOCUMENT_ROOT=$DOCUMENT_ROOT \ -DPATH_TRANSLATED=$PATH_TRANSLATED \ -DQUERY_STRING=$QUERY_STRING \ -DCONTENT_LENGTH=$CONTENT_LENGTH \ $CLASS else echo Content-type: text/html echo 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\> fi
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.
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.