Setting and Getting JavaBean's Properties

This section provides a tutorial example on how to use 'setProperty' and 'getProperty' action elements to to save and retrieve values of JavaBean properties.

As you can see from previous sections, a JavaBean is just a regular Java object. Once a JavaBean is created in a JSP, you can use JSP setProperty and getProperty action elements to set and retrieve values of its properties. To support these action elements, a JavaBean class must implement set and get methods based on some rules.

In this section, let's look at some of the basic rules about setting and getting JavaBean properties:

To validate above rules, I wrote the following sample JavaBean class:

/* DemoBean.java
 * Copyright (c) 2002 HerongYang.com. All Rights Reserved.
 */
package herong;
public class DemoBean {
   private String author = "Herong";
   private int count = 0;
   private boolean status = true;
   private String total = "1";
   private String size = "2";
   public void setAuthor(String a) {
      author = a;
   }
   public String getAuthor() {
      return author;
   }
   public void setCount(int c) {
      count = c;
   }
   public int getCount() {
      return count;
   }
   public void setStatus(boolean s) {
      status = s;
   }
   public boolean getStatus() {
      return status;
   }
   public void setTotal(int t) {
      total = "int: "+t;
   }
   public void setTotal(double t) {
      total = "double: "+t;
   }
   public String getTotal() {
      return total;
   }
   public void setSize(int s) {
      size = "int: "+s;
   }
   public void setSize(String s) {
      size = "String: "+s;
   }
   public String getSize() {
      return size;
   }
   public void setX(String x) {
      author = x;
   }
   public String getY() {
      return author;
   }
}

Compile this JavaBean code and deploy the class file to the Tomcat class folder:

herong> javac herong\DemoBean.java

herong> xcopy herong \local\tomcat\webapps\ROOT\WEB-INF\classes\herong

Now we are ready to test this JavaBean with a JSP page, DemoBean.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<!-- DemoBean.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:useBean id="b" class="herong.DemoBean"/>
<jsp:setProperty name="b" property="author" value="Nobody"/>
Line 1: author =
<jsp:getProperty name="b" property="author"/><br/>
<!-- <jsp:getProperty name="b" property="Author"/><br/> -->
<!-- <jsp:getProperty name="b" property="AUTHOR"/><br/> -->

<jsp:setProperty name="b" property="status" value="false"/>
Line 2: status =
<jsp:getProperty name="b" property="status"/><br/>

<jsp:setProperty name="b" property="count" value="5"/>
Line 3: count =
<jsp:getProperty name="b" property="count"/><br/>

<!-- <jsp:setProperty name="b" property="total" value="9"/> -->
Line 4: total =
<jsp:getProperty name="b" property="total"/><br/>

<jsp:setProperty name="b" property="size" value="14"/>
Line 5: size =
<jsp:getProperty name="b" property="size"/><br/>

<jsp:setProperty name="b" property="x" value="Herong"/>
Line 6: size =
<jsp:getProperty name="b" property="y"/><br/>
</body></html>
</jsp:root>

Deploy this JSP to Tomcat server. Then open this JSP page with a Web browser, you will get:

Line 1: author = Nobody
Line 2: status = false
Line 3: count = 5
Line 4: total = 1
Line 5: size = String: 14
Line 6: y = Herong

Note that:

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

JavaBean Objects and "useBean" Action Elements

 What Is a JavaBean

 "jsp:useBean" Action Elements

 "jsp:useBean" Requires Fully Qualified Class Name

 Servlet Class Converted from UseBean.jspx

Setting and Getting JavaBean's Properties

 Using JavaBean Objects in Scripting Elements

 Using Java Objects as JavaBeans

 getProperty() Error on Tomcat 7

 Refreshing Loaded JavaBean Classes

 Importing Unnamed Package Class Error

 Using JavaBean without Import Element Error

 Creating JavaBean Classes in Named Packages

 "NoClassDefFoundError" Exception

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials

 References

 Full Version in PDF/EPUB