• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on jsp:useBean from HFS

 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 356 of HFS, states the following question

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

Name : <jsp:getProperty name="person" property="name"/>


And the question # 3)

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


is said to give an output of "Evan" - the reason being that, control won't enter the body of useBean tag if type attribute is used.

When i tried the same example, the following is what i get
"javax.servlet.ServletException: bean person not found within scope"

However, when i have the useBean tag as

<jsp:useBean id="person" type="foo.Employee" scope="request">

i get "Evan" as the output. I dont find a mention of this in Errata as well.
Am i missing something here?

Env :Tomcat 5.0.30/Win XP
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

When you dont specify the scope the scope defaults to page scope.

The bean has been set in the request scope . Hence the first scenario
doesnt work since there is no bean with the specufied name in the page scope

However when we specify scope as re
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

When you dont specify the scope the scope defaults to page scope.

The bean has been set in the request scope . Hence the first scenario
doesnt work since there is no bean with the specufied name in the page scope

However when we specify scope as request , then it is able to find the required bean since we had set it into session
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiva,

Appreciate your quick response.

There is no scope mentioned in HFSJ question - so shouldn't there be an exception raised ? However, the book states that the above would work with "Evan" being the output.

The act of introducing scope="request" was my attempt to make it work.
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

This has been pointed out in the errata of HFSJ

I had the same doubt in fact but once i looked at errata it was clear

Please have a look at that chapter's errata
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic