Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
66
Chapter 3 Handling the Client Request: Form Data
the data one way for 
GET
 requests (in traditional CGI, this is usually through
the 
QUERY_STRING
 environment variable) and a different way for 
POST
requests (by reading the standard input in traditional CGI). Second, you have
to chop the pairs at the ampersands, then separate the parameter names (left
of the equal signs) from the parameter values (right of the equal signs).
Third, you have to URL decode the values. Alphanumeric characters are sent
unchanged, but spaces are converted to plus signs and other characters are
converted to %XX where XX is the ASCII (or ISO Latin 1) value of the char 
acter, in hex. Then, the server side program has to reverse the process. For
example, if someone enters a value of  
~hall, ~gates, and ~mcnealy
into a textfield with the name 
users
 in an HTML form, the data is sent as
users=%7Ehall%2C+%7Egates%2C+and+%7Emcnealy
 , and the
server side program has to reconstitute the original string. Finally, the fourth
reason  that  parsing  form  data  is  tedious  is  that  values  can  be  omitted  (e.g.,
param1=val1¶m2=¶m3=val3
 ) or a parameter can have more
than one value (e.g.,  
param1=val1¶m2=val2¶m1=val3
 ), so
your parsing code needs special cases for these situations.
3.2 Reading Form Data from 
Servlets
One of the nice features of servlets is that all of this form parsing is handled
automatically. You simply call the 
getParameter
 method of the 
Http 
ServletRequest
, supplying the case sensitive parameter name as an argu 
ment. You use 
getParameter
 exactly the same way when the data is sent by
GET
 as you do when it is sent by 
POST
. The servlet knows which request
method was used and automatically does the right thing behind the scenes.
The return value is a 
String
 corresponding to the URL decoded value of
the first occurrence of that parameter name. An empty 
String
 is returned if
the parameter exists but has no value, and 
null
 is returned if there was no
such parameter. If the parameter could potentially have more than one value,
you should call 
getParameterValues
 (which returns an array of strings)
instead of 
getParameter
 (which returns a single string). The return value of
getParameterValues
 is 
null
 for nonexistent parameter names and is a
one element array when the parameter has only a single value. 
Parameter names are case sensitive so, for example, 
request.get 
Parameter("Param1")
 and 
request.getParameter("param1")
 are
not interchangeable. 
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved