Using the <jsp:getProperty> Action
Once
you have created a bean and given it a name using the
<jsp:useBean> action, you can get the
bean’s property values with another JSP standard
action, named
<jsp:getProperty>
.
This action obtains the current value of a bean property and inserts
it directly into the response body.
To include the current fileName property value in
the page, simply use this tag:
<jsp:getProperty name="cartoon" property="fileName" />
The name attribute, set to
cartoon, refers to the specific bean instance
declared by the <jsp:useBean> action. The
<jsp:getProperty> action locates this bean
and asks it for the value of the property specified by the
property attribute. In Example 6-1, the property value is used as the
src attribute value for an HTML
<img> element. The result is the page shown
in Figure 6-1. The way this bean is implemented,
the fileName property value changes every time you
access the property; when you reload the page, a different cartoon
strip is shown.
![]() |
One thing in Example 6-1 may look a bit strange: an
element (<jsp:getProperty>) is used as the
value of another element’s attribute (the
<img> tag’s
src attribute). While this isn’t
valid HTML syntax, it is valid JSP syntax. Remember that everything that’s not recognized as a JSP element is treated as ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
