Talking to CGI Programs and Servlets
CGI stands for Common Gateway Interface; it is an API for writing applications (often scripts) that can be run by a web server to service a particular range of URLs. Servlets are an implementation very similar to CGI using a component-ized framework in Java. CGI programs and servlets can perform dynamic activities like automatically generating web documents. More important, they can accept data sent from the browser; they are most frequently used to process forms.
The name/value pairs of HTML form fields are encoded by the
client web browser in a special format and sent to the application
using one of two methods. The first method, using the HTTP command
GET
, encodes the user’s input into the
URL and requests the corresponding document. The server recognizes
that the first part of the URL refers to a program and invokes it,
passing along the information encoded in the URL as a parameter. The
second method uses the HTTP command POST to ask
the server to accept the encoded data and pass it to the CGI program
as a stream.
In Java, we can create a URL that refers to a CGI program and send it
data using either the GET or
POST methods. Why would we want to talk to a CGI?
Well, CGI remains a widely used technique for building web
applications. Other techniques such as opening sockets or talking via
RMI are coming on strong, but CGI has been in widespread use for
several years. Another important reason for using CGI is that many
firewalls block socket ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access