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

Parent reference can't access child method but whats happenning here? CLEARED THANKS.

 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

From K&B,
Given >>>
package sri.wcd;
public class Person {
private String name;
public Person(){
super();

}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
public class Employee extends Person {
private String empID;
public Employee(){
super();
}
public String getEmpID(){
return empID;
}
public void setEmpID(String id){
empID = id;
}
}
-------------------------------------
view.jsp
<jsp:useBean id="per" type="sri.wcd.Person" class="sri.wcd.Employee" scope = "page"><BR>
<jsp:setProperty name="per" property = "*"/><BR>
</jsp:useBean><BR>

Person Name:<jsp:getProperty name="per" property="name"/>
Employee ID:<jsp:getProperty name="per" property="empID"/> [How this works?]
--------------------------------------
INPUT: http://localhost:9080/ProjectOne/view.jsp?name=srinivasan&empID=12345
OUTPUT Person Name:srinivasan Employee ID:12345
--------------------------------------
Doubt Description:
Employee Object stored in Person reference.
Then How setProperty is going to find "empID(Employee)" through Person reference

[ April 14, 2007: Message edited by: Srinivasan thoyyeti ]
[ April 16, 2007: Message edited by: Srinivasan thoyyeti ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody Please reply ... It looks this thread got deserted by ALL.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srinivasan thoyyeti:
Doubt Description:
Employee Object stored in Person reference.
Then How setProperty is going to find "empID(Employee)" through Person reference


First of all, in your example there's a Person reference that 'refers' to an Employee object: this is not exactly the same as "Employee Object stored in Person reference". The Employee object is 'stored' somewhere in the heap, and there is a Person reference (in the stack) that 'refers' to it. That's it, more or less.

For example, the following statement in a jsp:


would translate to something like:


Note that the method that sets the attribute is PageContext.setAttribute, which takes a String and an Object parameters. So, when you create the bean it doesn't matter what the type reference is, as it finally will be treated as an Object. Finally, the setProperty action will know nothing about the reference you used when setting the attribute and so it will use the object's real class.

I hope my explanation is clear.
[ April 14, 2007: Message edited by: Sergio Tridente ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sergio,

Its quite a contradiction found in your explantion to fallowing K&B book Bullet point:

If you specify a "type" attribute in <jsp:useBean>, you can set properties in <jsp:setProperty> ONLY on properties of "type", but noton properties that exist in the actual "class" type. (In otherwords, polymorphism and normal Java type rules apply


Please reply.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say that that statement (given as it is and maybe out of its context) is not right accordingly to:

1) Tomcat's behaviour (but in this case it could be a problem in tomcat)

2) JSP 2.0 specs (these cannot be wrong, as they are the LAW). Take what this extract from JSP.5.2 from jsp 2.0 specs says, talking about jsp:setProperty action:


There are two variants of the jsp:setProperty action. Both variants set the values of one or more properties in the bean based on the type of the properties. The usual bean introspection is done to discover what properties are present, and for each, its name, whether it is simple or indexed, its type, and the setter and getter methods.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sergio,

I am totally confused.
1)Tell me what it really meant by fallowing bullet given in K&B if possible with an example.

If you specify a "type" attribute in <jsp:useBean>, you can set properties in <jsp:setProperty> ONLY on properties of "type", but noton properties that exist in the actual "class" type. (In otherwords, polymorphism and normal Java type rules apply


2)And whats the Actual Instrospection done.?

Please takem me out of .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using getProperty, the container will use Reflection to find the getter method.
If you're looking for "empID", the container will look for a method called "getEmpID" in your bean.
The bean may look like a Person from the outside, but it's still an Employee.
Using Reflection, you can actually access all public methods from Employee, even if the variable you hold is a Person.

Here is something you can try (what can be called usual bean introspection):
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivasan,

Here is something that I found written by the co-author of HFSJ book, have a look at itClick here. Probably this may help you.

Thanks,
-Rancy
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added an entry in the FAQ :
http://faq.javaranch.com/view?JspParameterStandardActions
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancy Chanda,

So sweet. Thanks a lot.
Now things are clear.

to Satou kurinosuke: Thanks for the reflection.

Now i am out of Desert.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic