aspose file tools*
The moose likes JSP and the fly likes Polymorphism Rules and the <jsp:useBean> tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Polymorphism Rules and the <jsp:useBean> tag" Watch "Polymorphism Rules and the <jsp:useBean> tag" New topic
Author

Polymorphism Rules and the <jsp:useBean> tag

Meenakshi Sahu
Greenhorn

Joined: Jan 19, 2010
Posts: 1
Hi,



The polymorphism rules must work in the jsp as well while we are using the <jsp:javaBean> tags.I read in one book as well that : " IF YOU SPECIFY THE 'TYPE' ATTRIBUTE IN JSP <JSP:USEBEAN> YOU CAN SET THE PROPERTIES IN <JSP:SETPROPERTY> ONLY ON THE PROPERTIES OF THE 'TYPE' , BUT NOT ON THE PROPERTIES THAT EXIST ONLY IN THE ACTUAL 'CLASS' TYPE.(IN OTHER WORDS THE POLYMORPHISM AND JAVA RULES APPLY.)


I tried to test this using the below jsp :

<BODY>
<jsp:useBean id="person" type="principal.Person"
class="principal.Employee" scope="request">
<jsp:setProperty name="person" property="name" value="Meenakshi" />
<jsp:setProperty name="person" property="empId" value="10063865" />
</jsp:useBean>
Employee Name:<jsp:getProperty name="person" property="name" />
Employee ID:<jsp:getProperty name="person" property="empId" />
</BODY>


And my Person and Employee classes are as below :

public abstract class Person {
String name = "";
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}



public class Employee extends Person {
private Integer empId;
public Integer getEmpId() {
return empId;
}

public void setEmpId(Integer empId) {
this.empId = empId;
}

}



However it allows me to set the values for both the name and the empId attribute, though the empId is not present in teh Person class.
And i get the output on my page as Employee Name:Meenakshi Employee ID:100.
Can anyone explain me why its happening so?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Hi Meenakshi,

Welcome to java ranch. Please do not use ALL CAPS when writing a post. It will appear like you are shouting. Please put your code inside the CODE tags so that they are readable.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism Rules and the <jsp:useBean> tag