It's not a secret anymore!
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes page 360 if property= Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "page 360 if property="*"" Watch "page 360 if property="*"" New topic

page 360 if property="*"

Satish Saravanan
Ranch Hand

Joined: Jun 30, 2006
Posts: 41
In page 360,

<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="*"/>

In foo.Person we have "name" property & in foo.Employee we have "empID" property.

The request parameter names in the html are "name" and "empID".

The JSP handwriteen say's that
"I want you to iterate through the request parameters, and find any that matches this bean's
property names and set the value of the matching properties equal to the value of the corresponding request paramaeter."

My understanding of the above is that both the properties of the bean (name and empID)will be set.

Is it wright.
But in page 414 BULLET POINTS it says that
"If you specify a 'type'attribute in <jsp:useBean>,you can set properties in <jsp:setProperty> ONLY on properties of the 'type' ,but NOT on properties that exist only in the actual 'class' type."

since the type of <jsp:useBean> is foo.Person and since it contains only name property.
ONLY it will be set.empID will not be set.

Is it Wright.

PLEASE explain me.
Sreeraj G Harilal
Ranch Hand

Joined: Apr 19, 2006
Posts: 310
I am also confused.
This code also working.

<%@ page import="foo.*" %>
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name"/>
<jsp:setProperty name="person" property="empID" value="<%= new Integer(request.getParameter("empID")).intValue()%>"/>
<jsp:getProperty name="person" property="name"/>
<jsp:getProperty name="person" property="empID"/>

SCJP 5.0<br />SCWCD 1.4<br />Preparing for <b>SCEA</b>.<br /><b>"I prefer an interesting vice to a virtue that bores."</b>
Bryan Basham
Ranch Hand

Joined: Apr 30, 2001
Posts: 199
Hello Satish,

Page 360 of HFS is correct. The jsp:setProperty action, when used with property="*", will iterate over all request parameters and attempt to set the property within the JavaBean, irrespective of the 'type' as declared in the jsp:useBean tag. The jsp:setProperty action uses introspection to find the setter methods for each request parameter; and introspection ignores the "declared type" of the object and deals directly with the "actual class" of the object. Make sense?

The penultimate (second to the last) bullet on page 414, is not correct.

However, that bullet is right in one respect that wasn't highlighted in the book. When the jsp:useBean action is used, the JSP page will now include a local variable in the generated service() method, which does follow polymorphic rules.

For example, the JSP code:

Is roughly equivalent to the following scriptlet code:

Notice that the declared type is the superclass, Person, but the created object is of type Employee.

What do you think this code would do?

If you guessed, that a compile-timer error is thrown, then you are correct. Java "typing rules" are in effect even within a JSP page.

[ December 05, 2006: Message edited by: Bryan Basham ]
Anupama Ponnapalli
Ranch Hand

Joined: Jun 12, 2006
Posts: 66
Hi Bryan,

Is this (page 414 bullet) going to be included in the errata?

I could not find it here:

There have been a number of posts on this topic, and it will definitely help.

Bryan Basham
Ranch Hand

Joined: Apr 30, 2001
Posts: 199
Hello Anupama,

I have submitted an errata entry for this mistake.

Thanks for the suggestion,
Vinod Subramaniyam

Joined: Mar 19, 2007
Posts: 26
Hi All,
<jsp:getProperty name="person" property="empId"/> does not work. So we can take the statement "the property accesed depends on type" for jsp:getProperty. But as pointed earlier the jsp:setProperty property="*" sets all the Properties eventhough type does not have the property.
<jsp:useBean type="Person" class="Employee">
I agree. Here's the link:
subject: page 360 if property="*"
It's not a secret anymore!