File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Whats the error??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Whats the error???" Watch "Whats the error???" New topic

Whats the error???

Bhavna Jharbade
Ranch Hand

Joined: Sep 08, 2005
Posts: 69
I have a doubt in the following piece of code from HFSJ pg 356.

jsp code is.....

<jsp:useBean id="person" type="foo.Employee">
<jsp:setProperty name="person" property="name" value="Fred"/>
Name is:<jsp:getProperty name="person" property="name"/>

servlet code is........

foo.Person p=new foo.Employee();

Person is an abstract bean class n Employee extends it.

Now this code wudnt work. I want to know why... as far as i cud understand is as we have not mentioned the scope in useBean tag, by default it wud be page. In the servlet we have set the person attribute at request scope, so useBean wont find any attribute named person in page scope n thus give error.Am i understanding it correctly...If anyone can help???

Thanx in advance.

SCJP1.4 (86%)
Paolo Metafune
Ranch Hand

Joined: Aug 22, 2005
Posts: 34
Yes you are right.
Another consideration:
<jsp:setProperty> as used in this example will never be executed, also if you put your bean in page scope. It is only executed when a bean isn't in scope and you use the class or beanName attribute to create or load an istance of the bean.

Bhavna Jharbade
Ranch Hand

Joined: Sep 08, 2005
Posts: 69
Paolo thanx for the confirmation. But now if i have this code in my servlet wud it work......

foo.Employee p=new foo.Employee();

The book says it will work but according to me it shud again not work because of the reason the earlier code didnt worked. What do u say???
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 951
Though you are setting attribute of the same type, the scope is different, so the useBean doesn't find the attribute. Also, only type is defined in usBean so no new object will be created. If you define the "scope" attribute to "request", you have to forward the same request from servlet code to target jsp.

Hope it help you.


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Troy Peter

Joined: Oct 13, 2005
Posts: 14
Hi Bhavna,

The great HFSJ book also has errors. Thankfully there's an errata page...please consult it whenever you think the book is not making sense.

Here's the link:

Good luck,
Bhavna Jharbade
Ranch Hand

Joined: Sep 08, 2005
Posts: 69
Hi Troy,
Thanx for the link it has cleared my doubt.

Thanx to others also for their help.
Sub swamy
Ranch Hand

Joined: Oct 02, 2002
Posts: 121
Just to confirm,

The example shown in Pg. 356 with

would NOT work. Isn't that the conclusion ?
Bhavna Jharbade
Ranch Hand

Joined: Sep 08, 2005
Posts: 69
Yes n the reason is same as is for the first piece of code.Its a mistake in the book.
I agree. Here's the link:
subject: Whats the error???
jQuery in Action, 3rd edition