jsp:useBean action tag:
jsp:useBean action tag is used to instantiate a bean class. It first search for an existing instance using id and scope variables. If object is not found than it creates bean class object.
Commonly used attributes of jsp:useBean:
1. id: This attribute is used to uniquely identify the bean class within the specified block.
2. scope: This attribute represents the scope of the bean. Scope may page (default scope), request, session or application.
3. class: This attribute specifies the full package name of the bean.
Syntax:
<jsp:useBean id="beanName" class="package.class"/>
jsp:setProperty action tag:
jsp:setProperty action tag is used to set the value of the specified bean property.
Commonly used attributes of jsp:setProperty:
1. name: This attribute refers to the bean class.property: This attribute specifies the bean property whose value is to be set.
2. value: This attribute specifies the value set to the bean property.
Syntax:
<jsp:setPropertyname="beanName" property="propertyName" value=”propertyValue“/>
jsp:getProperty action tag:
jsp:getProperty action tag is used to get the value of the specified bean property and insert it into output.
Commonly used attributes of jsp:getProperty:
1. name: This attribute refers to the bean class.
2. property: This attribute specifies the bean property whose value is to be retrieve.
Syntax:
<jsp:getPropertyname="beanName" property="propertyName"/>
Example:
test.jsp
<html> <head> <title>useBean,setProperty and getProperty action example</title> </head> <body> <h4>Enter student detail.</h4> <form action="display.jsp" method="post"> Name: <input type="text" name="name"><br/><br/> RollNo: <input type="text" name="rollNo"><br/><br/> <input type="submit" value="Sign up"> </form> </body> </html> |
StudentBean.java
/** * This class acts as a java bean for a student. * @author w3spoint */ public class StudentBean { //student properties private String name; private String rollNo; //no-argument constructor public StudentBean(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRollNo() { return rollNo; } public void setRollNo(String rollNo) { this.rollNo = rollNo; } } |
display.jsp
<html> <head> <title>useBean,setProperty and getProperty action example</title> </head> <body> <h4>Student detail:</h4> <jsp:useBean id="student" class="com.w3spoint.business.StudentBean"/> <jsp:setProperty name="student" property="*"/> Name:<jsp:getProperty name="student" property="name"/><br> RollNo:<jsp:getProperty name="student" property="rollNo"/><br> </body> </html> |
web.xml
<web-app> <welcome-file-list> <welcome-file>test.jsp</welcome-file> </welcome-file-list> </web-app> |
Output:
Enter Name and RollNo:
Click on Sign up button:
Download this example.
Next Topic: Exception handling in JSP with example.
Previous Topic: jsp:param action tag with example.