I am trying to study 'jsp:useBean' and found that 'jsp:setProperty' and 'jsp:getProperty' is used in association with useBean. My doubt is, why do we need these action tags when we can directly call the setter and getter methods using bean id.?
I did a sample to test it.
Bean:
package test.usebean.bean;
public class UseBeanTarget {
@Override
public String toString() {
return "UseBeanTarget [userName=" + userName + ", password=" + password
+ "]";
}
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String displayName(){
return userName;
}
}
JSP:
<jsp:useBean id="targetBean" class="test.usebean.bean.UseBeanTarget"></jsp:useBean>
<jsp:setProperty property="userName" name="targetBean" value="Renjith"/>
<jsp:setProperty property="password" name="targetBean" value="ren@1234"/>
<h2>
Set using setProperty
<br />
<%= targetBean %>
</h2>
<hr />
<%
targetBean.setUserName("Renjith_Direct");
targetBean.setPassword("ren$1234");
%>
<h2>
After setting the properties directly
<br />
<%= targetBean.getUserName() %>
<br />
<%= targetBean.getPassword() %>
</h2>
What i observed is that both serves the same purpose.
Result:
Set using setProperty
UseBeanTarget [userName=Renjith, password=ren@1234]
After setting the properties directly
Renjith_Direct
ren$1234