JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

Using JavaBean Classes

Part:   1  2  3  4  5 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

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

  • The setProperty element must be supported by at least one set method in the JavaBean class.
  • The set method name must be the property name with the first lower case letter being translated to upper case, and prefixed with "set". For example, "setAuthor" is a good method name to support the setProperty action element for property name "author".
  • The return type of the set method should be void.
  • The set method should take only one input parameter.
  • If there are multiple set methods, there must be one that takes String as the input parameter type, and this will be the one to be used by the setProperty action element.
  • If there is only one set method, the input parameter type should be a primitive type.
  • The get method name must be the property name with the first lower case letter being translated to upper case, and prefixed with "get". For example, "getAuthor" is a good method name to support the getProperty action element for property name "author".
  • The get method should take no input parameter.
  • The return type of the get method can be String or any primitive type.
  • JavaBean proproty names must be started with a lower case letter.

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

/**
 * DemoBean.java
 * Copyright (c) 2003 by Dr. Herong Yang. 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 source code with JDK 1.4.1, and copy the class file to the Tomcat class path. Remember to store the class file under a sub directory named as "herong".

>\local\j2sdk1.4.1\bin\java DemoBean.java
>copy DemoBean.class 
   \local\jakarta-tomcat-4.1.18\webapps\root\web-inf\classes\herong

Now we are ready to test this JavaBean with an JSP page, DemoBean.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- DemoBean.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<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>

Make sure to run your Tomcat under JDK 1.4.1. Then open this JSP page with IE, 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

(Continued on next part...)

Part:   1  2  3  4  5 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - Using JavaBean Classes