How to Communicating with cgi forms
So you've seen how Java applets can read data from
cgi programs. getting information about forms based
things needs a little more coding.
There are two ways of sending data to forms
- GET - the data is encoded in the URL
/cgi-bin/finger?postmaster
gets finger information for the postmaster. This way of
talking to cgi programs is fast becoming obsolete as
the urls can get pretty long with complex queries.
- POST - data is not encoded in the url, but is sent
on the standard input of the cgi-program. This is a lot more versatile
and is what will be used here.
The protocol for talking to cgi-bin scripts is:
- create a URL.
- open a connection to the URL.
- get an output stream from the connection. This output stream is connected
to the standard input stream of the cgi-bin script on the server.
- write to the output stream.
- close the output stream.
- get any data returned on the standard output stream of the cgi-bin
script
You might need to encode the data sent to the cgi-bin using the
URLEncoder
class. This is because most cgi-scripts
expect to get data in a MIME format called
x- www-form-urlencoded
.
In order to run this example, you need to install the following two
programs into the cgi-bin directory of your httpd server. (sorry
only for servers that have perl)
- timestable.pl - a cgi-script that outputs
the times table.
- www_lib.pl - A library of perl functions
that make decoding forms easier.
OK I've done it lets go on