×
☰ See All Chapters

usebean action tag in JSP

JSP pages are easy and straightforward, but there are some disadvantages.

  • JSP pages will lose their readability because of spaghetti-like code. 

  • JSP pages are not having separation between UI code and business logic code. Developer should be aware of both HTML and Java. We cannot spit the work among HTML developers and Java Developers. 

To split the development among HTML developers and Java Developers would be nice approach from productivity point of view. (Productivity means completing more work in less time.)

In JSP, this is possible through the use of JSP components, such as JavaBeans. In this approach, the Java programmer writes and compiles JavaBeans classes incorporating necessary functionality of the application. While the programmer is developing JavaBeans, the UI (User interface)/HTML developer can continue with page designing work. When the JavaBeans are ready, the UI developer uses tags related to JavaBeans similar to HTML tags to call methods and properties of the beans from the JSP page. (Here UI developer should be aware of some non HTML tags).

Rules for JavaBean class

  • It should have a no-arg constructor. 

  • It should be Serializable. 

  • It should provide methods to set and get the values of the properties, known as getter and setter methods. 

In JSP, the jsp:getProperty and jsp:setProperty action elements are used to invoke a getter and a setter method, respectively. jsp:seBean is used to specify the bean which you going to use in JSP.

Example for JavaBean

public class UserBean {

        private String firstname;

        private String lastname;            

        private String email;

       

        public String getFirstname() {

                return firstname;

        }

        public void setFirstname(String firstname) {

                this.firstname = firstname;

        }

        public String getLastname() {

                return lastname;

        }

        public void setLastname(String lastname) {

                this.lastname = lastname;

        }

        public String getEmail() {

                return email;

        }

        public void setEmail(String email) {

                this.email = email;

        }

}

<jsp:useBean>

To use bean class in JSP, bean should be available, To make the bean available, use the jsp:useBean action element.

<jsp:useBean> Syntax

<jsp:useBean id="idForTheBean" beanName = "NameForTheBean" class="fullyQualifiedBeanClassName" scope="beanScope" type="typeCasting" >

 

The four attributes that can be used in a jsp:useBean action element are as follows:

  1. id 

  2. class 

  3. type 

  4. scope 

id

It is a unique identifier for the bean. It should be unique and can be used as the object reference for the bean.

class

The class attribute specifies the fully qualified name for the JavaBean class. A fully qualified name is not required if the bean's package is imported using the page directive, however.

type

It specifies the type of the JavaBean class. The type of the bean could be the type of the class itself, the type of its superclass, or an interface the bean class implements. This attribute is very rarely used.

scope

Defines the scope of the bean. This attribute can take one of the following values:

  1. page : The bean can be used within the page. 

  2. request: The bean can be used till the response is given. 

  3. session: The bean can be used till the session expiry. 

  4. application : The bean can be used till the server shutdown. 

The default value of the scope attribute is page.

Example for jsp:useBean action tag

Project directory structure

jsp-usebean-0
 

DateBean.java

package com.java4coding;

public class DateBean {

        private String date;

 

        public String getDate() {

                return java.util.Calendar.getInstance().getTime().toString();

        }

}

demo.jsp

<html>

<body bgcolor="yellow">

        <jsp:useBean id="dateObj" class="com.java4coding.DateBean" />

 

        <%

                String date = dateObj.getDate();

                out.print("Today is: " + date);

        %>

</body>

</html>

Output

jsp-usebean-1
 

Accessing Properties Using jsp:getProperty and jsp:setProperty

In JSP world the variables of JavaBean class are called as properties.

jsp:getProperty

jsp:getProperty is used to access the property of a bean. For this to work there should be getter method in bean class.

Syntax of jsp:getProperty

 

<jsp:getProperty name="idOfUseBeanActionTag" property="propertyName"/>

 

The name attribute must be assigned to the value of id attribute of jsp:useBean. The property attribute must be assigned the name of the property.

  • A jsp:getProperty element returns the property value converted into String. The return value is then automatically fed into an out.print method so it will be displayed in the current JSP page. 

  • Example 

<jsp:useBean id="dateObj" class="com.java4coding.DateBean" />

<jsp:getProperty name="dateObj" property="date"/>

jsp:setProperty

  • The jsp:setProperty action element is used to set the value of a property. 

  • Syntax of the jsp:setProperty: 

        <jsp:setProperty name="idOfUseBeanActionTag" property="PropertyName" value="value"/>

The following example demonstrates the use of the jsp:getProperty and jsp:setProperty action elements.

Example for using jsp:getProperty and jsp:setProperty

Project directory structure

jsp-usebean-2
 

UserBean.java

package com.java4coding;

 

public class UserBean {

        private String firstName;

        private String lastName;

        public String getFirstName() {

                return firstName;

        }

        public void setFirstName(String firstName) {

                this.firstName = firstName;

        }

        public String getLastName() {

                return lastName;

        }

        public void setLastName(String lastName) {

                this.lastName = lastName;

        }

}

demo.jsp

<html>

<body bgcolor="yellow">

        <jsp:useBean id="beanObj" class="com.java4coding.UserBean" />

       

        <jsp:setProperty name="beanObj" property="firstName" value="Manu"/>

        <jsp:setProperty name="beanObj" property="lastName" value="Manjunatha"/>

       

        First Name: <jsp:getProperty name="beanObj" property="firstName"/> </br>

        Last Name: <jsp:getProperty name="beanObj" property="lastName"/>

</body>

</html>

Output

 

jsp-usebean-3
 

All Chapters
Author