JSP usebean getproperty setproperty

jsp:useBean action tag:

jsp:useBean action tag is used to instantiate a bean class. It first searches for an existing instance using id and scope variables. If the object is not found then it creates a bean class object.

 

Commonly used attributes of jsp:useBean:

  1.  id: This attribute is used to identify the bean class within the specified block uniquely.
  2. scope: This attribute represents the scope of the bean. The scope may be 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 will 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 will be retrieved.

 

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 W3schools360
 */
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>