Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
13.3 Setting Bean Properties
299
Associating Individual Properties with Input
Parameters
Setting the
itemID
property was easy since its value is a
String
. Setting the
numItems
and
discountCode
properties is a bit more problematic since
their values must be numbers and
getParameter
returns a
String
. Here is
the somewhat cumbersome code required to set
numItems
:
<%
int numItemsOrdered = 1;
try {
numItemsOrdered =
Integer.parseInt(request.getParameter("numItems"));
} catch(NumberFormatException nfe) {}
%>
name="entry"
property="numItems"
value="<%= numItemsOrdered %>" />
Fortunately, JSP has a nice solution to this problem that lets you associate
a property with a request parameter and that automatically performs type
conversion from strings to numbers, characters, and boolean values. Instead
of using the
value
attribute, you use
param
to name an input parameter. The
value of this parameter is automatically used as the value of the property, and
simple type conversions are performed automatically. If the specified input
parameter is missing from the request, no action is taken (the system does
not pass
null
to the associated property). So, for example, setting the
numItems
property can be simplified to:
Figure 13 2 Result of SaleEntry1.jsp.
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
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