Handling the Client Request:
Form Data
Example URL at online travel agent
http://host/path?user=Marty+Hall&origin=iad&dest=nrt
Names (user) come from HTML author;
values (Marty+Hall) usually come from end user
Parsing form (query) data in traditional CGI
Read the data one way for GET requests, another way for
POST requests
Chop pairs at &, then separate parameter names (left of
the "=") from parameter values (right of the "=")
URL decode values (e.g., "%7E" becomes "~")
Need special cases for omitted values
(param1=val1&
param2=
¶m3=val3) and repeated
params (
param1=val1
¶m2=val2&
param1=val3
)
27
Servlets
www.corewebprogramming.com
Reading Form Data
(Query Data)
getParameter("name")
Returns value as user entered it. I.e., URL decoded value
of first occurrence of name in query string.
Works identically for GET and POST requests
Returns null if no such parameter is in query
getParameterValues("name")
Returns an array of the URL decoded values of all
occurrences of name in query string
Returns a one element array if param not repeated
Returns null if no such parameter is in query
getParameterNames()
Returns Enumeration of request params
28
Servlets
www.corewebprogramming.com
14