CGI Programming in Java
Java Programming Resources home. Core Web Programming home.
Core Web Programming Sources These examples cover using Java for both the client and the server side of the CGI process. The client-side part covers using GET and POST from applets to talk to CGI programs (regardless of what language the CGI programs are written in). The server-side part covers implementing CGI programs in Java that handle GET and POST (regardless of whether the client uses HTML forms or applets), and also includes a URL decoder and CGI form parser in Java (and a similar parser for cookie values). The examples are extracted from Chapters 17 and 18 of Core Web Programming from Prentice Hall. 1996-99 Marty Hall.

If you are new to CGI, I very strongly recommend that you consider Java servlets and JavaServer Pages (JSP) instead of "regular" CGI. Please see my tutorial on Java servlets and JSP 1.0 for more detail.

For more info on CGI, see the CGI book shelf.

CGI Client-Side Examples

These examples are taken from chapter 17. The chapter also includes complete coverage of all the HTML FORM elements, ISINDEX, ISMAP, and methods for sending GET and POST data from Java applets. Chapter 16 covers HTTP, cookies, and public-key cryptography. Chapters 19 and 20 cover JavaScript, with specific examples on using JavaScript to validate FORM values before they are submitted, processing cookies on the client instead of on the server, calling Java from JavaScript, and calling JavaScript from Java. See the source code archive for example code.
Java Source Description On-Line Example
SearchYahoo.java. Requires SearchService.java. A simple example of an applet as a CGI client that works like an HTML form, sending GET data and having the browser display the results. This one talks to the Yahoo! search engine. SearchYahoo.html
SearchExcite.java. Requires SearchService.java. Another example of an applet as a CGI client. This one talks to the Excite search engine, demonstrating one advantage of using Java instead of forms: you can more easily reuse code in other applications (this one extends the same class that the Yahoo applet did). SearchExcite.html
ShowFile.java An applet that sends data via GET and then reads the results itself (rather than having the browser display results). ShowFile.html
Weather.java. The client-side (applet) requires CityChooser.java and WeatherPanel.java. The server-side is answered by the WeatherInfo script, which then invokes WeatherInfo.java. An applet that sends data via POST and then reads the result. Weather.html

CGI Server-Side Examples

These examples come from chapter 18. The chapter also includes complete coverage of all the CGI environment variables, a URL decoder, CGI parser, and Cookie parser in Java, server-side Java and the Servlet API, and a quick overview of non-CGI alternatives such as NSAPI, ISAPI, LiveWire, and JDBC. JDBC is covered in depth in chapter 15. Servlets are a particularly good alternative for people who are installing/customizing their own server, or whose employer's or ISP's server already supports them. You should very seriously consider using them instead of "standard" CGI with Java if you fall in this category. Please see my tutorial on Java servlets and JSP 1.0 for more detail.
Shell Script Interface
(Unix Specific)
Java Source
(Portable)
Description On-Line Example
CgiHello N/A An extremely simple CGI Script that outputs "Hello, World". CgiHello
ShowData N/A A simple CGI script that shows any attached data. ShowData
CgiGet CgiGet.java. Requires CgiShow.java. Note that some browsers will try to interpret the HTML strings in the print statements and the result may be formatted strangely when viewed in the browser. But you can save the file to disk to edit it normally. A simple script that passes the query data from the QUERY_STRING variable to a Java program that builds a page showing the data supplied. CgiGet
CgiCommandLine CgiCommandLine.java A simple script that passes the command-line data to a Java program that builds a page showing the data supplied. Arguments are separated by plus signs ("+") and cannot contain an equals sign ("="). CgiCommandLine
IsIndex IsIndex.java A script that passes the data to a Java program that builds an HTML document that uses ISINDEX. Data-entry page or results page is built depending on whether any data is supplied. IsIndex
CgiPost CgiPost.java A script that invokes a Java program without passing any data to it. The program reads data from standard input. CgiPost.html
ShowParse ShowParse.java. Requires QueryStringParser.java, CgiParser.java, LookupTable.java, URLDecoder.java, and StringVector.java. A script that passes data to a Java program that separates the various attached variables, URL decodes their values, and produces a table of the results. Can accept GET or POST. ShowParse version using GET. See below for a version that sends POST data.
ShowParse ShowParse.java. Requires QueryStringParser.java, CgiParser.java, LookupTable.java, URLDecoder.java, and StringVector.java. A script that passes data to a Java program that separates the various attached variables, URL decodes their values, and produces a table of the results. Can accept GET or POST. AdBuilder.html; sends POST data to ShowParse. See above for a version that sends GET data.
CssTest CssTest.java. Requires CssChoices.java, CookieParser.java, CgiParser.java, LookupTable.java, and URLDecoder.java. A script that passes data to a Java program that builds a page to let you test out cascading style sheet properties. Properties selected are stored as cookies, which are parsed and used as defaults in later sessions. CssTest

Java is a trademark of Sun Microsystems. The original of this document can be found at http://www.apl.jhu.edu/~hall/java/CGI-with-Java. 1996-1998 Marty Hall (hall@apl.jhu.edu).