Java Reference
In-Depth Information
Rather than require fancier browsers with more dynamic querying or other ca-
pabilities, Web servers became smarter and were able to talk to other programs
that would generate HTML on the fly and send it back as the response to an
incoming request. In the Java environment, this mechanism includes the
Servlet and related classes.
As for requests coming from a browser, they come in two flavors— GET
and POST . The GET request is a request via a URL. Simple URLs that appear
as hyperlinks on a Web page are sent as GET requests. Any additional parameters
appear at the end of the URL as name=value pairs separated by “ & ”. The
parameters are separated from the URL with a “ ? ” character:
http://www.google.com/search?hl=en&ie=ISO-8859-1&q=java
The example URL includes three parameters:
hl=en
ie=ISO-8859-1
q=java
The POST is virtually the same, except that the name=value pairs don't
appear on the URL but are sent in a less visible way. The net result is the same,
and the same methods can be used in the servlet to retrieve the parameters. The
POST requests typically come from HTML form elements, as when you fill in
the fields of a form and press a submit button (though forms can specify that
the browser use GET as the submission mechanism for a particular form). The
biggest advantage to posting the form is that the parameters don't appear in
the URL, which is both more aesthetically pleasing and avoids problems from
accidentally revisited pages or user-altered parameters.
One further twist: URLs are not necessarily literal paths to files anymore.
The Web server can interpret parts of the URL as an alias for some other pro-
gram. So http://www.google.com/search may not actually refer to a direc-
tory named search on the Google site, but more likely tells the Web server to
use its search program. We'll discuss this more in Chapter 19.
So servlets are given requests which have come from browsers (and other
Web clients), and then they respond with output. In our examples, we'll be
sending HTML back. There are lots of other choices, too. Since browsers un-
derstand other formats, a servlet might also send back plain text or even image
data. Another choice gaining popularity is having the servlet generate XML and
then using a conversion via stylesheets to produce HTML. This allows for the
Search WWH ::




Custom Search