Java Reference
In-Depth Information
in form fields before sending them to a Web server. You can see that in some
URLs—space gets replaced with a “ + ” character, and special characters (such
as the plus sign) get replaced with a character sequence for hexadecimal values
(for example, “ + ” becomes %2B ). The getParameter() method will automat-
ically decode those. But we need to remember this if we want to generate any
literal URLs in the HTML that we produce. (See the URLEncoder class in the
Javadoc documentation for servlets.)
One more annoyance that must be dealt with: What if the URL contains
the same argument twice—for example, www.google.com/search?
cmd=search&cmd=bogus ?
If you make the call to getParameter() you will get the first value
( search ). If you want to handle such a situation differently, you can call
getParameterValues() which will return an array of String s for all the
different values. In our example,
String [] allofem = getParameterValues("cmd");
will return an array such that:
allofem[0] = "search"
allofem[1] = "bogus"
If there was only one value, then you get an array of one element. If the
parameter wasn't used in the URL, getParameterValues() returns null .
18.6
M ATTERS OF S TATE : C OOKIES , H IDDEN V ARIABLES ,
AND THE D READED “B ACK ” B UTTON
The toughest part about working with HTML is, perhaps, its statelessness .
HTML and browsers were not designed to keep a connection going. It's not a
phone call type of connection, where the line is kept open between the browser
and the Web server. Rather, it's a one-shot, send-me-what-you've-got mecha-
nism more like postal mail (but without the stamp). Here's the rub: Just be-
cause you mail a letter, you can't assume that you'll get an answer back. There
is no on-going connection between browser and server, except for the duration
of the data transfer. Once you've got your complete page displayed, the
Search WWH ::




Custom Search