The Polyglot Web

Web Coding in Java

Java is a compiled language. However, it does not compile to native code like C++ does, but to byte code that runs on the Java Virtual Machine (JVM). Because Java does not compile to native code, I invoke the Java interpreter from the command line in a separate executable shell script file, for example this one, which is named images.cgi:

java Images

CGI from Java

Using Java to implement CGI is no longer preferred practice. Instead, it is recommended to use JSP and Servlets. This requires using Tomcat and linking to some specialized CGI libraries. I cannot currently include or link to these libraries, so I'm sticking to old-school CGI.

The internet resources date back to 1995–2000, and they are not very useful. Some articles explain the process, but the code files are gone. Most use an additional shell script file that invokes the Java interpreter and also passes along the required CGI variables, such as QUERY_STRING and CONTENT_LENGTH.

I found that these various CGI variables are available as environment variables that can be obtained from a separate process invoked from within Java using its Runtime library. So, the shell script files are very simple. The Java code is organized as a separate class that can be used in other projects.