There are a few limitations with this method however. One limitation is that the GET method, which uses the environment variable QUERY_STRING to hold the passed data can NOT be used. This means that links such as /cgi-bin/myprogram?name=Gene&email=user@somewhere.com is not going to work since that is the GET method. The reason it doesn't work is that there is no way (I know of) to pass this data when executing the .class directly. (There is a way by going through a shell first, see below.) The second problem with this method is that if your class file is in a Java package, I don't know how to run it directly from the command line, let alone as a CGI. For example, if you make a java program "foo" as part of the package "bar" then you have to run it (traditionally) as "java foo.bar". Under Linux, if the java program "bar" were NOT in the package "foo", you could run it by simply saying "bar.class". But directly running java classes that are contained in packages escapes me.
So if you want to use GET as well as POST methods you must resort to making your cgi program start a shell, set some environment variables and then launch the java interpreter (runtime) with the appropriate args so that it will run the script you want. See below.
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 .classin 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