• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in <jsp:useBean> tag

 
Jay Shukla
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.



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 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.
 
nisha chidella
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.
 
Harshana Dias
Ranch Hand
Posts: 348
  • 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: 348
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic