• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Question on jsp:useBean from HFS

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic