Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
76
Chapter 3 Handling the Client Request: Form Data
Listing 3.6 shows the servlet that processes the data from the HTML form.
When the  Preview  button is pressed, the servlet first reads the font and color
parameters. Before using any of the parameters, it checks to see if the value is
null
 (i.e., there is an error in the HTML form and thus the parameter is missing)
or is an empty string (i.e., the user erased the default value but did not enter any 
thing in its place). The servlet uses a default value appropriate to each parameter
in such a case. Parameters that represent numeric values are passed to
Integer.parseInt
. To guard against the possibility of improperly formatted
numbers supplied by the user, this 
Integer.parseInt
 call is placed inside a
try
/
catch
 block that supplies a default value when the parsing fails. Although it
may seem a bit tedious to handle these cases, it generally is not too much work if
you make use of some utility methods such as 
replaceIfMissing
 and 
repla 
ceIfMissingOrDefault
 in Listing 3.6. Tedious or not, users will sometimes
overlook certain fields or misunderstand the required field format, so it is critical
that your servlet handle malformed parameters gracefully and that you test it with
both properly formatted and improperly formatted data.
Core Approach
Design your servlets to gracefully handle missing or improperly formatted 
parameters. Test them with malformed data as well as with data in the 
expected format.
Once the servlet has meaningful values for each of the font and color
parameters, it builds a cascading style sheet out of them. If you are unfamiliar
with style sheets, they are a standard way of specifying the font faces, font
sizes, colors, indentation, and other formatting information in an HTML 4.0
Web page. Style sheets are usually placed in a separate file so that several
Web pages at a site can share the same style sheet, but in this case it is
more convenient to embed the style information directly in the page by
using the
STYLE
 element. For more information on style sheets, see
http://www.w3.org/TR/REC CSS1
.
After creating the style sheet, the servlet places the job applicant's name,
job title, and e mail address centered under each other at the top of the page.
The heading font is used for these lines, and the e mail address is placed
inside a 
mailto:
 hypertext link so that prospective employers can contact
the applicant directly by clicking on the address. The programming languages
specified in the 
languages
 parameter are parsed using 
StringTokenizer
(assuming spaces and/or commas are used to separate the language names)
and placed in a bulleted list beneath a  Programming Languages  heading.
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