

 |
|

When Java Meets CGI
By Jay Lorenzo
Both Java and ActiveX have fundamentally changed how we interact with the Internet by introducing a higher level of interactivity and functionality than was previously possible. I believe this level of interactivity will grow even further, with the emergence of a new breed of applications that rely on these technologies to be integrated into both client- and server-side components. The Jeeves API, which has recently been introduced by Sun, is an example of this trend.
As exciting as some of these changes may be, it is still important to realize that in terms of interactivity, a great many sites rely on server-based CGI programs to provide compatibility with the widest number of browser platforms. As Java and ActiveX become more common in Web use, there will be many more instances where it will be necessary to maintain compatibility with preexisting server-side CGI programs. Java-enabled pages can be an integrated part of this environment, and in the process can ensure that data submitted contains the correct formatting and data validation before being submitted to the server.
There are some limitations to this approach. Sun has built several security restrictions into Java that will affect us when we wish to use applets for network communications. Applets loaded over the Internet are typically only permitted to communicate with the server from which they were downloaded. This means that if you want to communicate with other servers, you will most likely rely on a proxy service or another CGI back-end process on the originating host to accomplish this. In this column, we will examine how to use Java's networking capabilities to communicate via the POST method to an existing server-side CGI program.
Using the Java.Net.* Classes
Sun has provided a rich set of class libraries in java.net.* that make it extremely easy to provide network functionality in applets. These classes provide a quick way to create communication using TCP and UDP sockets and URL objects, without relying on platform-specific APIs to do so. For a review of these classes, point your browser to http://www.javasoft.com/products/JDK/CurrentRelease/api/ for the current API release.
How can we create code that simulates the steps taken by a browser to do a POST action to a CGI program? As you may be aware, a POST is usually handled by the browser if the
|