Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
13.3 Setting Bean Properties
295
Figure 13 1 Result of StringBean.jsp.
13.3 Setting Bean Properties
You normally use
jsp:setProperty
to set bean properties. The simplest
form of this action takes three attributes:
name
(which should match the
id
given by
jsp:useBean
),
property
(the name of the property to change), and
value
(the new value).
For example, the
SaleEntry
class shown in Listing 13.3 has an
itemID
property (a
String
), a
numItems
property (an
int
), a
discountCode
prop
erty (a
double
), and two read only properties
itemCost
and
totalCost
(each of type
double
). Listing 13.4 shows a JSP file that builds an instance of
the
SaleEntry
class by means of:
The results are shown in Figure 13 2.
Once the bean is instantiated, using an input parameter to set the
itemID
is straightforward, as shown below:
name="entry"
property="itemID"
value= <%= request.getParameter("itemID") %> />
Notice that I used a JSP expression for the
value
parameter. Most JSP
attribute values have to be fixed strings, but the
value
and
name
attributes of
jsp:setProperty
are permitted to be request time expressions. If the
expression uses double quotes internally, recall that single quotes can be used
instead of double quotes around attribute values and that
\'
and
\"
can be
used to represent single or double quotes within an attribute value.
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