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

doubt on jsp:useBean tag

 
mimi chatterjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a doubt on the <jsp:useBean> tag from HFSJ.


class Person--having String getName() and void setName(String)
this is an abstract class.

class Employee--having int getEmpID() and void setEmpID(int)
this is a child class of Person.

Now
if a servlet having lines,
Person p = new Employee();
p.setName("Evan");
request.setAttribute("person",p);

then forward it to a JSP containing the line,
<jsp:useBean id="person" type="Employee">
<jsp:getProperty name="person" property="name" />
</jsp:useBean>


the book says that this will fail at request time.But if we change the servlet code to Employee p = new Employee();
it will run properly.
My qns is why the former code i.e.
Person p = new Employee(); is not right.

can any body help me please??
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is because the reference variable 'p' is of type Person eventhough the actual object being pointed by is of type Employee. Obviously a reference variable of type Parent class will have no idea on the methods available in its sub classes.

A subclass can refer its super class members and NOT the reverse!
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the code is wrong because you put an person in request scope.
Your bean action is trying to retrieve person from page scope.

If you only declare type attribute in bean action, it only works if the bean already exists.
 
mimi chatterjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Raghaban,
hi,
I am accessing the methods of the parentclass not of the subclass.So in that case i think no problem should occur.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. I thought it was on the subclass .

You can check for the scope as mentioned by Antonio. If you leave out the scope, it is by default "page" scope. That may cause the issue!
 
Satyendra Sharna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sub Class can't hold the refference of Super class.

Person p = new Employee() is vilation of inheritence.
 
mimi chatterjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Satyendra

Hi,
if Person is the superclass of Employee then i think the foellowing code is not wrong..

Person p = new Employee();
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Person p = new Employee(); is correct.
Person can be a class , an interface or an astract class.

Could you tell me the page where it appears?
 
mimi chatterjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Antonio

HFSJ book(1st edition)--page no--356
 
sudhakar karnati
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the book says that this will fail at request time.But if we change the servlet code to Employee p = new Employee();
it will run properly.


mimi chatterjee,

Even if you change the code as you said you will get error because the code
<jsp:useBean id="person" type="Employee">
looks for object in the page scope and it could not find..
To make the above code correct just scope attribute should be added with the value as "page"..and i think there is nothing wrong with the servlet code written..But if we try to create an object of type Person then it is an error..

Thanks & Regards,
Sudhakar Karnati
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got HFSJ 2nd Edition.
They just say that the following will not compile
<jsp:useBean id=�person� type=�foo.Employee� class=�foo.Person�/>

It's true, because you can't instantiate Person: it's an abstract class
( Employee emp=new Person(); // compile error )
 
Karthick Thiagarajan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@sudhakar

I think the default scope is page so there is no need to add that right? but here the person attribute is stored in request scope right?
 
sudhakar karnati
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@sudhakar I think the default scope is page so there is no need to add that right? but here the person attribute is stored in request scope right?


yes.Here the person attribute is bound to request but the useBean looks only in page scope(default)..If you want to make the above code work just add scope attribute with value as request that should work and there is nothing wrong with the object creation and reference type in Servlet code..

Thanks & Regards,
Sudhakar Karnati
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm curious if it will work if you add scope='page' to <jsp:usebean ..> !

The attribute is stored as a Person object, not as an Employee object. So if you declare the type as Employee, a cast is needed (in the translated servlet) on the attribute to promote it to an Employee.

Note that the HFSJ example has a Person as type, not an Employee.

I'm not sure about this, so like I said, I'm curious about the outcome.
 
sudhakar karnati
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm curious if it will work if you add scope='page' to <jsp:usebean ..> !


I think when we add scope="request" to <jsp:useBean id="person" type="Employee">it should work as per HFSJ first edition as mentioned on the page number 416 bullet number one.

Note that the HFSJ example has a Person as type, not an Employee.


In HFSJ type is mentioned as Employee..This is what given in the book
<jsp:useBean id="person" type="foo.Employee">
I dont know which version you are refering to..I am looking at HFSJ First Edition..

I tried to add the scope attribute to the <jsp:useBean> tag but it is not working..

Here is the code:
SimpleServlet.java



result.jsp:


I am getting this error:
java.lang.ClassNotFoundException: Employee

I am getting the same error if i change the type as "Person" in the useBean

Thanks & Regards,
Sudhakar Karnati
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhakar, I was refering to p.353/354. Maybe HFSJ made a mistake on p.416
>Edit: I see you're right, the question is about p.356/416

Anyway, did you use the fully qualified name as type, i.e. include the package name? That may have caused the CNF exception.


If I do thiseverything works out fine.

Also changing the type to Employee makes no difference.
[ June 27, 2008: Message edited by: Jan Sterk ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic