Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ Chapter 8 useBean question

 
Alba Rea
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Here is an exercise from chapter 8:

*****************************************************************

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

Name is <jps:getProperty name="person" property="name"/>

<!-- combined with this servlet code -->

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

*******************************************************************

According to the book this will work, but I don't understand how.

The standard action part only defines type and the scope is not defined, from my understanding then a person attribute has to exsit in page scope.

But de servlet part has added a person attribute into request scope, how can container find this attribute in page scope? Any help here? Thanks.

Alba
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

this question has been asked many times
i think there is errata on this page
even after reading the errata, this page is unclear for me
i have tried many times to understand it, but it simply
 
Alba Rea
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Niranjan, I found the errata page of this book. Now I understand why.

Alba
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alba,

could you please explain me the thing ?
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet code sets an attribute name "person" in the request object. To retrive the attribute we have to use <jsp:useBean> and it should search the attribute in the requestScope.
But in the useBean when the "type" is available without the "class" the bean should exist otherwise it results in an exception.
So it happens here because "scope" is not here in the <jsp:useBean> and so it defaults to page. So it only searches in the pageScope for an bean instance and it results in an exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic