PR: ? I: ? L: ? LD: ? I: ? Rank: ? Age: ? I: ? whoissource Density

CGI Programming in Java

You may be asking yourself, how in the hell can I successfully implement CGI programming in Java, handling both GET and POST data? Well, here's the answer - at least for Linux and Solaris platforms running the the Apache server.
Here's my shell script cgi-test
#!/bin/sh
#
# the above starts the shell. The shell you choose will have important 
# ramifications later. This one uses a simple shell, probably bash. 
# Most real humans prefer tcsh, but it isn't found everywhere and the 
# idiots at Sun don't offer much support for it, so this example uses sh
#


#
# This next stuff is required of any cgi program to identify what the
# mime type of the output is. Here we will generate text. Note that 
# all of these lines that start with "echo" could be put into your 
# java application instead. They were put here to aid in debugging. 
# It is going to say "Hello" and then list out some environment 
# variables.
#
echo "Content-type: text/html\n\n";
echo "";
echo "Hello";

echo "content_type = $CONTENT_TYPE \n";
echo "content_length = $CONTENT_LENGTH \n";
echo "request_method = $REQUEST_METHOD \n";
echo "query_string = $QUERY_STRING \n";
echo "server_name = $SERVER_NAME \n";
echo "server_port = $SERVER_PORT \n";
echo "script_name = $SCRIPT_NAME \n";

#
# Just for the hell of it, I make the shell list out the enviroment
# variables.
#

env

#
# The last line you should see before the java starts
#
echo "executing java script now";

#
# We are going to launch the java interpreter (runtime) so it is 
# important that we tell it what the CLASSPATH is. Note your CLASSPATH
# will NOT be the same as mine. You have to figure it out yourself.
# Basically its where your .class files are stored, but there is a whole
# new ugly convention for building "packages"
#
# To set your CLASSPATH variable, keep in mind the shell you are using
# in tcsh, you would say "setenv CLASSPATH /usr/local/...."
# and you wouldn't need the export line. In this crappy shell, you do
# whats shown below.
#
CLASSPATH=/usr/local/java/lib/classes.zip:/usr/me/projects:.
export CLASSPATH


#
# Finally you run the java interpreter with the following arguments
# The arguments should guarantee that all the GET variables arrive
# safely. The POST variables are read from the stdin so they should
# be handled by the java program itself (I think thats how it works)
# My java program was called CGITest and is the package Misc
#
/usr/local/java/bin/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 \
  Misc.CGITest

#
# Then we quit the shell nicely
#
exit 1;

Lastly?, a gentleman named AndYmaN has told me that he had to add these lines to his Apache configuration files. I maintain that I didn't have to add anything to them, but just in case, here is what AndYmaN did.

in srm.conf add the line

AddHandler cgi-script .class
in mime.types add the line (this one is probably already there. You may just have to add the "class" word at the end) application/octet-stream bin dms lha lzh exe class


Return to Gene's Home Page
Return to Gene's Random Unix Crap