Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whats the error???

 
Bhavna Jharbade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"/>
</jsp:useBean>
Name is:<jsp:getProperty name="person" property="name"/>


servlet code is........

foo.Person p=new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);

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.
 
Paolo Metafune
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bye.
 
Bhavna Jharbade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paolo thanx for the confirmation. But now if i have this code in my servlet wud it work......

foo.Employee p=new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);

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
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks
 
Troy Peter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed

Good luck,
Troy
 
Bhavna Jharbade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Troy,
Thanx for the link it has cleared my doubt.

Thanx to others also for their help.
 
Sub swamy
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to confirm,

The example shown in Pg. 356 with


would NOT work. Isn't that the conclusion ?
 
Bhavna Jharbade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes n the reason is same as is for the first piece of code.Its a mistake in the book.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic