23 - JSP Java Beans

23.1 Overview of JSP Java Beans

Java Beans are the simple plain Java classes that follows Java bean specifications. There are certain rules which all java beans have to follow-

a) Class must have a default argument constructor.

b) All fields or properties should be defined with private access specifier.

c) Corresponding to each property, define a public getter and setter method.

23.2 Rules for Getter/Setters

a) if the property name is X then its getter method will be defined as getX() {method will start with get followed by property name after capitalized first character}.

b) corresponding setter method will be setX().

c) getter method will return a value of type same as property and must return the value of property.

d) setter method will take one argument of type field and will set the field with the given value in argument.

23.3 JSP Java Beans Example

Create a class Account with two fields a) Account number b) balance

package com.sample.jsp.tutorial;
public class Account {
    private String accountNumber="000000";
    private int  balance;
    public Account() {
    }
    public String getAccountNumber() {
        return accountNumber;
    }
    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
}

23.4 Access Java Beans in JSP

23.4.1 <jsp:useBean>

JSP Specification provides <jsp:useBean> to interact with java beans in JSP. The jsp:useBean action tag searches for an existing java bean object based on id in a given scope. If object does not found, it creates a the java bean.

General syntax of <jsp:useBean> is

<jsp:useBean
  id= "instanceName" 
  scope= "page | request | session | application"       
  class= "packageName.className" 
</jsp:useBean>

Where

· id is the identifier used to identify the bean in a given scope.

· class attribute is used to instantiate the specified bean. Bean must have zero argument constructor and must not be an abstract class.

· scope is scope of bean and can have four possible values.

o requestspecifies that bean is available in all jsp (sharing same request).

o page- specifies that this bean can be used within the page only and this is default scope.

o session- bean is available in session scope.

o application- bean is available in application scope.

Create a bean.jsp in WebContent directory with below content.

<html>
  <head>
    <title> Use Bean Example </title>
  </head>
  <body>
    <jsp:useBean id="account" class="com.sample.jsp.tutorial.Account" scope="request">
    </jsp:useBean>
    Account Number :: <%=account.getAccountNumber() %>
    <br/>    
    Account Balance ::  <%=account.getBalance() %>
  </body>
</html>

Access bean.jsp using http://localhost:8080/jsp-tutorial/bean.jsp

Note : If jsp:useBean tag is used with a body, the content of the body is only executed if the bean is created. If the bean already exists in the given scope, the body is skipped.

By term “Used with Body” we mean

<jsp:useBean id=”someID” class=”someClass” scope=”request’>
….
….
</jsp:useBean>

23.4.2 <jsp:getProperty>

This action is used to get the value of attribute of a java bean instance and it displays the value of the property. Syntax of getProperty tag is

     <jsp:getProperty name=”bean_name” property=”property_name” />

Where name is the name of java bean and property is the name of attribute of bean.

Lets change the bean.jsp created in earlier section to use <jsp:getProperty> tag in place of scriplets. Changes are highlighted below

bean.jsp updated code

<html>
  <head>
   <title> Use Bean Example </title>
  </head>
  <body>
   <jsp:useBean id="account" class="com.sample.jsp.tutorial.Account" scope="request">
   </jsp:useBean>
   Account Number :: <jsp:getProperty property="accountNumber" name="account"/>
   <br/>    
   Account Balance ::  <jsp:getProperty property="balance" name="account"/>
  </body>
</html>

23.4.3 <jsp:setProperty>

This action is used to set the value of attribute of a java bean instance. <jsp:setProperty> tag can set the value directly from the request parameter or can set explicit value or even can set all the request parameters with all bean attributes (provided bean attributes and request parameter names matches).

The syntax of <jsp:setProperty> tag are –

    · <jsp:setProperty name=”bean_name” property=”property_name” value=”explicit_value” />

This form of <jsp:setProperty> will populate property provided in ‘property’ attribute of bean given in ‘name’ attribute with the value given in ‘value’ attribute.

     · <jsp:setProperty name=”bean_name” property=”property_name” param=”request param” />uest param” />

This form of <jsp:setProperty> will populate property provided in ‘property’ attribute of bean given in ‘name’ attribute with the value of the request parameter given in ‘value’ attribute.

    · <jsp:setProperty name=”bean_name” property=”property_name” />

This form of <jsp:setProperty> will populate property provided in ‘property’ attribute of bean given in ‘name’ attribute with the value of the request parameter having same name as attribute name.·

<jsp:setProperty name=”bean_name” property=”*” />/>

This form of <jsp:setProperty> will populate all the properties of bean given in ‘name’ attribute with the values of the request parameters having same name as attribute names.

Update the bean.jsp tag to use <set:Property> tag

<html>
  <head>
   <title> Use Bean Example </title>
  </head>
  <body>
   <jsp:useBean id="account" class="com.sample.jsp.tutorial.Account" scope="request">
   </jsp:useBean>
   Account Number :: <jsp:getProperty property="accountNumber" name="account"/>
   <br/>    
   Account Balance ::  <jsp:getProperty property="balance" name="account"/>
   <jsp:setProperty name ="account" property="accountNumber" value ="123456" />
   <jsp:setProperty name ="account" property="balance" value ="1000" />
   <br/>
   Account Details after "jsp:setProperty" tags
   <br/>
   Account Number :: <jsp:getProperty property="accountNumber" name="account"/>
   <br/>    
   Account Balance ::  <jsp:getProperty property="balance" name="account"/>
  </body>
</html>

Access bean.jsp using http://localhost:8080/jsp-tutorial/bean.jsp

Like us on Facebook