aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doubt on jsp:useBean tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doubt on jsp:useBean tag" Watch "doubt on jsp:useBean tag" New topic
Author

doubt on jsp:useBean tag

mimi chatterjee
Greenhorn

Joined: Dec 03, 2007
Posts: 26
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

Joined: Apr 20, 2006
Posts: 3344

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!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
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.


SCJP 5, SCWCD 5
mimi chatterjee
Greenhorn

Joined: Dec 03, 2007
Posts: 26
@ 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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Jun 01, 2008
Posts: 5
Sub Class can't hold the refference of Super class.

Person p = new Employee() is vilation of inheritence.
mimi chatterjee
Greenhorn

Joined: Dec 03, 2007
Posts: 26
@ 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

Joined: Jun 05, 2008
Posts: 110
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

Joined: Dec 03, 2007
Posts: 26
@ Antonio

HFSJ book(1st edition)--page no--356
sudhakar karnati
Ranch Hand

Joined: May 03, 2007
Posts: 93
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

Joined: Jun 05, 2008
Posts: 110
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

Joined: Feb 02, 2007
Posts: 14
@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?


SCJP1.4 (81%)<br />SCWCD1.5(95%)
sudhakar karnati
Ranch Hand

Joined: May 03, 2007
Posts: 93

@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

Joined: Jun 06, 2008
Posts: 139
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.


SCJP 1.4 (81%)<br />SCWCD 5 (95%)
sudhakar karnati
Ranch Hand

Joined: May 03, 2007
Posts: 93
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

Joined: Jun 06, 2008
Posts: 139
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on jsp:useBean tag