Communicating with CGIs and Servlets Through GET

The URL class makes it easy for Java applets and applications to communicate with server-side CGI programs and servlets that use the GET method. (CGI programs and servlets that use the POST method require the URLConnection class and will be discussed in Chapter 15.) All you need to do is determine what combination of names and values the program expects to receive, then cook up a URL with a query string that provides the requisite names and values. All names and values must be x-www-form-url-encoded as by the URLEncoder.encode( ) method discussed in the last section.

There are a number of ways to determine the exact syntax for a query string that talks to a particular CGI or servlet. If you’ve written the server-side program yourself, you already know what name-value pairs it expects. If you’ve installed a third-party program on your own server, the documentation for that program should tell you what it expects.

On the other hand, if you’re talking to a program on a third-party server, matters are a little trickier. You can always ask people at the remote server to provide you with the specifications for talking to their CGI programs. However, even if they don’t mind you doing this, there’s probably no one person whose job description includes “telling third-party hackers with whom we have no business relationship exactly how to access our servers”. Thus, unless you happen upon a particularly friendly or bored individual who has ...

Get Java Network Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.