<jsp:useBean> action element :-


jsp:useBean action element is used to create an object of bean class. It has the following syntax -

<jsp:useBean id="bean_object_name" class="package_name.class_name" type="package_name.class_name" 
scope="page | request | session | application"  beanName="package_name.class_name" />        
Where -
i- id - is name of bean class object.
ii- class - is fully qualified bean class name, which determines object of which bean class to be created. It also defines created bean object will stored in which reference if type attribute is not defined..
iii- type - type attribute if present defines the reference of which class in which created object will stored.
iv- scope - it defines the accessibility level of bean object. It can have one of the following values -
  • page - The bean will be available only on the current jsp page where jsp:useBean is defined. A new object of bean will be created for each page request and it will be destroyed automatically when page lose focus i.e. moves to some other page.
  • request - The bean will be available on the current page where jsp:useBean is defined as well as on the requested page (using jsp:forward or jsp:include action element).It has wider scope than page.
  • session - The bean will be available on all the pages which have participated in session management. It has wider scope than request.
  • application - The bean will be available on all the pages available in web app. It has wider scope than session.
The default scope for jsp:useBean is Page.
beanName - It defines name for the bean that the instantiate method of the java.beans.Beans class expects.
Note - The bean object will be available in jsp page only after jsp:useBean. It will not be available before that point.

Example -

This simple example will show you how to use jsp:useBean to create bean class object.
1- Create a package named beans and within that create a bean class named Marks as follows -

package beans;


public class Marks 
{
    private int physics , maths;

    public int getPhysics() {
        return physics;
    }

    public void setPhysics(int physics) {
        this.physics = physics;
    }

    public int getMaths() {
        return maths;
    }

    public void setMaths(int maths) {
        this.maths = maths;
    }
   
}        
2- Create another bean class named Student that will inherit Marks class as follows -

package beans;


public class Student extends Marks
{
    private int roll;
    private String name;

    public int getRoll() {
        return roll;
    }

    public void setRoll(int roll) {
        this.roll = roll;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}        
3- index.html - This will be your default page.
<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form method="post" action="ShowData.jsp">
            Student ROll Number - <input type="text" name="t1"><br>
            Student Name - <input type="text" name="t2"><br>
            Physics Marks - <input type="text" name="t3"><br>
            Maths Marks - <input type="text" name="t4"><br>
            <input type="submit" value="Enter">
        </form>
    </body>
</html>   

4- ShowData.jsp - Create a ShowData.jsp file as follows -

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        
        <jsp:useBean id="std1" class="beans.Student" scope="request"></jsp:useBean>
        <%
            int rl = Integer.parseInt(request.getParameter("t1"));
            String name = request.getParameter("t2");
            int ph = Integer.parseInt(request.getParameter("t3"));
            int mth = Integer.parseInt(request.getParameter("t4"));
            
        //set bean properties
        std1.setRoll(rl);
        std1.setName(name);
        std1.setPhysics(ph);
        std1.setMaths(mth);
        //get bean properties
        out.println(std1.getRoll()+"<br>");
        out.println(std1.getName() +"<br>");
        out.println(std1.getPhysics()+"<br>");
        out.println(std1.getMaths()+"<br>");
        %>
    </body>
</html>  

5- Now run the project, It will show you index.html page as follows -



Press enter button and it will redirect you to ShowData.jsp that will show you bean properties set by us -



Download this example (NetBeans implementation) - Click Here


Use of type attribute -

if type attribute is not available in <jsp:useBean> then class attribute defines the class of object created as well as class of reference variable in which object will store.
For Example -
<jsp:useBean id="std1" class="beans.Student" scope="request"></jsp:useBean>
In this case the bean class object and reference variable where that object will store, will be of Student type. At the time of translation of jsp into servlet, above jsp code will be translated into java as follows -
beans.Student std1
= null; //
beans.Student std1
- reference variable determines by class attribute if(std1 == null) { std1 =
new beans.Student();
//
new beans.Student()
- object of which bean class also determines by class attribute }
If type attribute is defined in <jsp:useBean> then class attribute defines the class of bean object and type attribute defines the class of reference variable in which object will store.
For Example -
<jsp:useBean id="std1" class="beans.Student" 
type="beans.Marks"
scope="request"></jsp:useBean>
At the time of translation of jsp into servlet, above jsp code will be translated into java as follows -
beans.Marks mk1
= null; //
beans.Marks mk1
- reference variable determines by type attribute if(mk1 == null) { mk1 =
new beans.Student();
//
new beans.Student()
- object of which bean class determines by class attribute }


Learn - <jsp:setProperty> and <jsp:getProperty> action elements