Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Head First Servlets and JSP:SCWCD Question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 8, page 356, BE the Container Exercise for JSP:USEBEAN

The question gives a standard action which is

Note: Person is an Abstract class and Employee is a concrete class of Person

<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"/>

Now this JSP is forwarded by a servlet that does some basic work.

Figure out what the JSP code would do for each of the different servlet code examples.

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

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

The book says, that in the second case it would print Evan in the JSP. However when I ran the same code, I get "Bean person not found within scope: java.lang.Instantiation exception"

Can any one explain why the book says that the JSP would fail in first case and succeed in second case?
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check Errate about this question you will find correct answer.
Answer given in book is not correct.
 
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:useBean id="person" type="foo.Employee" scope="request">
<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>


If we specify scope="request" its working!!
I don't know why its not checking the entire scope ie.(page->request->session->application) for the bean attribute person.
 
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everybody please check the errata list at http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed before posting anything on HSFJ errata...This is clearly mentioned in the list.

Well Sreeraj, as for your query, of course it will work if you include scope="request" in the useBean tag because the container finds that there is an attribute "person" present in request scope and does not go further to create a new one....Now, if you don't specify a scope in the useBean tag, default scope chosen is page and the container doesn't find an attribute "person" there....so it tries to create one and hell breaks loose, since we have not specified the class="...." attribute in the useBean tag....REMEMBER-You can choose to avoid the class="...." attribute and use only the type="..." attribute in the useBean tag if you're sure that the bean exists in the scope specified.
[ November 30, 2006: Message edited by: Sayak Banerjee ]
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sayak.

i thought <jsp:useBean..> check every scope automatically for the attribute like EL.

its worth than a book reading.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some more interesting facts about <jsp:useBean> and <jsp:setProperty>....you won't find these in HFSJ but a mere inspection of the generated servlet code would reveal all:-

You can use <jsp:setProperty> on its own without using <jsp:useBean>
eg. <jsp:setProperty name="person" property="name" value="Sayak" />

The way it functions doesn't depend on the usage of <jsp:useBean>...in the example above the container will look through all the scopes and set the property on the bean found at the scope to which it's bound....order of searching -page, request, session, application....BUT it will throw an exception if the bean doesn't exist in any of the scopes


Now that we know this.... if we use something like :
<%
foo.Employee p=new foo.Employee();
p.setName("Sayak");
pageContext.setAttribute("person",p);
%>
<jsp:useBean id="person" class="foo.Employee" scope="request">
<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>
with foo.Employee the same bean class as in HFSJ with "name" & "empID" properties...
Keepin' in mind that the bean "person" exists only in page scope then the result may not be as we normally think it would be....What happens is this:
When <useBean> is executed the container doesn't find the bean "person" in request scope so it creates one and binds it to request scope...but when <setProperty> is executed it starts searching for bean "person" in all the scopes atrting from page scope....it finds one at page scope first and sets the property "name" of that bean(and not the one in request scope)
Therefore
${requestScope.person.name} prints nothing
${pageScope.person.name} prints Fred
[ December 01, 2006: Message edited by: Sayak Banerjee ]
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic