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

Head First Servlets and JSP:SCWCD Question

 
Meenakshi Verma
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?
 
Gowher Naik
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.
 
Sreeraj G Harilal
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.
 
Sayak Banerjee
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic