JSP – useBean Action Tag

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:

  • id: This attribute is used to uniquely identify the bean class within the specified block.
  • scope: This attribute represents the scope of the bean. Scope may page (default scope), request, session or application.
  • 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:

  • name: This attribute refers to the bean class.
  • property: This attribute specifies the bean property whose value is to be retrieve.

Syntax:

<jsp:getPropertyname=”beanName” property=”propertyName”/>

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:

public class StudentBean
{
            private String name;
            private String rollNo;
            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=”StudentBean”/>
                                    <jsp:setProperty name=”student” property=”*”/>
                                    Name:<jsp:getProperty name=”student” property=”name”/><br>
                                    RollNo:<jsp:getProperty name=”student” property=”rollNo”/><br>
            </body>
</html>
Scroll to Top