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

Doubt in <jsp:useBean> tag

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am preparing for SCWCD and for that i'm using HFSJ (Head First Servlet and JSP)book.
In that book on P 420 they gave solution for "Be the Container" Exc.

Follwing is the Question:



What happens if the Servlet code are as follows:




Please note that in above code there are two classes "Person" (abstract class) and "employee"
concrete sub class of class "person" belongs to same package "foo".

methods of class person

==> String getName()
==> void setName(String)

methods of class employee

==> int getEmpID()
==> void setEmpID(int)


Ans Explanation (As per the given):

FAILS at request time! The "person" attribute is stored at the request scope, so the <jsp:useBean> tag won't work since it specifies only a type. The container KNOWS that if you have obly a type specified, there MUST be an existing bean attribute of that name and scope.
In above explanation "there MUST be......."

I'm not clear with that what should be there if i do not use class in that <jsp:useBean> tag.

Please help me.

Thanks in Advance.



 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This issue is raised in the unconfirmed errata. The useBean scope is set to request, so there should be no problem.
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe Thanks for the reply.

But my concern is i'm not clear with the concept of sentence "The container KNOWS that if you have obly a type specified, there MUST be an existing bean attribute of that name and scope. "

Please help me in understand above concept.

Thanks in Advance.
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

"The container KNOWS that if you have only a type specified, there MUST be an existing bean attribute of that name and scope. "



Does above quote mean that there must be another <jsp:useBean> tag with id="employee" ???

Please try to explaiin in brief because i'm really confuse.

Thanks.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when you have only type specified in <jsp:useBean>, the control will never go to line 2, because with no class specified, the bean instance cannot be created. so, the container knows that there must be a attribute for that bean i.e, employee with name "person" in the given scope i.e, request.




Does above quote mean that there must be another <jsp:useBean> tag with id="employee" ???



It means there must be a attribute named "person" with type employee. somewhere in the servlet there could be :

foo.employee e = new foo.employee()
request.setAttribute("person", e)

or in jsp

<jsp:useBean id="person" class="foo.employee" scope="request"/>
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nisha for your excellent explaination!!!

I'm almost clear.



foo.employee e = new foo.employee()
request.setAttribute("person", e)



In above codes you have mentioned foo.employee e = new foo.employee() but in the actual question person class's referance already points to object of employee object.

Hence it sould give the correct answer. Am i correct?

Thanks a lot for your excellent efforts.
 
nisha chidella
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hence it sould give the correct answer. Am i correct?



correct. It will print Evan.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So its basically because of there is a attribute in request scope(set by servlet) with type (reference type) Person not Employee gives the error right?
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nisha chidella wrote:


Hence it sould give the correct answer. Am i correct?



correct. It will print Evan.



How could it print Evan? I mean in JSP standerd action you have to use type="foo.Person" to print Evan..isnt it?
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just 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