In this Tutorial we want to describe you a code that help in describing an example from JSP bean set property. The code include a package bean , we have a class Employees. The employee class include a string variable first name, last name and address. Inside the class we have a setter and getter method.
The JSP page uses this getter and setter method.
The < jsp:use Bean> instantiate a bean class and locate a bean class with specific scope and name.
For running this application we need to save the jsp bean getproperty.jsp in Tomcat/wepapps/.jsp.The required url is typed in the browser and the JSP page will be displayed.
jsp bean getproperty.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <html>
<body>
<h1>Get Value from bean</h1>
<jsp:useBean id="emp" class="bean.Employees" scope="page" />
<jsp:setProperty name="emp" property="firstName" value="Komal Singh"/>
<jsp:setProperty name="emp" property="lastName" value="Choudhary"/>
<jsp:setProperty name="emp" property="adddress" value="Delhi"/>
<table>
<tr>
<td>First Name</td>
<td> : </td>
<td> <jsp:getProperty name="emp" property="firstName"/> </td>
</tr>
<tr>
<td>Last Name</td>
<td> : </td>
<td> <jsp:getProperty name="emp" property="lastName"/> </td>
</tr>
<tr>
<td>Address</td>
<td> : </td>
<td> <jsp:getProperty name="emp" property="address"/> </td>
</tr>
</table>
</body> </html> |
package bean; public class Employees {
protected String firstName;
protected String lastName;
protected String address;
public void setAddress(String address) {
this.address = address;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
}
|
Output of the program

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: JSP bean set property View All Comments
Post your Comment