Java Reference
In-Depth Information
are discussed in
Chapter 7
.) All you need to know is what combination of names and
values the program expects to receive. Then you can construct 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 earlier in this
chapter.
There are a number of ways to determine the exact syntax for a query string that talks
to a particular program. If you've written the server-side program yourself, you already
know the 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. If you're
talking to a documented external network API such as the
eBay Shopping API
,
then the
service usually provides fairly detailed documentation to tell you exactly what data to
send for which purposes.
Many programs are designed to process form input. If this is the case, it's straightforward
to figure out what input the program expects. The method the form uses should be the
value of the
METHOD
attribute of the
FORM
element. This value should be either
GET
, in
which case you use the process described here, or
POST
, in which case you use the process
described in
Chapter 7
. The part of the URL that precedes the query string is given by
the value of the
ACTION
attribute of the
FORM
element. Note that this may be a relative
URL, in which case you'll need to determine the corresponding absolute URL. Finally,
the names in the name-value pairs are simply the values of the
NAME
attributes of the
INPUT
elements. The values of the pairs are whatever the user types into the form.
For example, consider this HTML form for the local search engine on my Cafe con
Leche site. You can see that it uses the
GET
method. The program that processes the form
is accessed via the URL
http://www.google.com/search
. It has four separate name-value
pairs, three of which have default values:
<
form
name
=
"search"
action
=
"http://www.google.com/search"
method
=
"get"
>
<
input
name
=
"q"
/>
<
input
type
=
"hidden"
value
=
"cafeconleche.org"
name
=
"domains"
/>
<
input
type
=
"hidden"
name
=
"sitesearch"
value
=
"cafeconleche.org"
/>
<
input
type
=
"hidden"
name
=
"sitesearch2"
value
=
"cafeconleche.org"
/>
<
br
/>
<
input
type
=
"image"
height
=
"22"
width
=
"55"
src
=
"images/search_blue.gif"
alt
=
"search"
border
=
"0"
name
=
"search-image"
/>
</
form
>
The type of the
INPUT
field doesn't matter. For instance, it doesn't matter if it's a set of
checkboxes, a pop-up list, or a text field. Only the name of each
INPUT
field and the
value you give it is significant. The submit input tells the web browser when to send the
data but does not give the server any extra information. Sometimes you find hidden
INPUT
fields that must have particular required default values. This form has three hid‐
den
INPUT
fields. There are many different form tags in HTML that produce pop-up