Following is the best example to understand the
JSP setProperty-getProperty.
Details.java
[java]
package com;
public class Details {
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;
}
}
[/java]
JSP setProperty-getProperty - Here created two text fields
username and
password, written methods
getUsername() and
getPassword() to retrieve the user name and password. For the data members the developer used private keyword means that encapsulation concept.
user.jsp
[java]
//useBean, getProperty and setProperty example
<form action="userdetails.jsp" method="post">
User Name: <input name="username" type="text" />
User Password: <input name="password" type="password" />
<input type="submit" value="register" />
</form>
[/java]
Here included userdetails.jsp file in form method.
userdetails.jsp
[java]<jsp:usebean class="com.Details" id="userinfo"></jsp:usebean>
<jsp:setproperty name="userinfo" property="*"</jsp:setproperty>
You have entered below details: You have entered below details:
UserName: <jsp:getproperty name="userinfo" property="username"></jsp:getproperty>
Password: <jsp:getproperty name="userinfo" property="password"></jsp:getproperty>[/java]
Output
Output will be as follows with two text fields one for username and another for password and register button also.
After entering the username and password output will be as follows.