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

This chapter explains:

  • How to load a JavaBean into JSP pages and manipulate its properties.
  • Compilation issues of using JavaBean classes in unnamed packages.
  • Setting and getting JavaBeans properties.
  • Using JavaBeans as Java objects in scripting elements.
  • Using Java objects as JavaBeans.
  • How to refresh the JavaBean objects loaded in memory.

The "jsp:useBean" Action Elements

jsp:useBean: A JSP action element that loads a JavaBean object into the JSP page.

<jsp:useBean id="object_name" class="class_name"/>

where "object_name" is the name of the object to be created, and "class_name" is the class name of the JavaBean class from which the object will be instantiated.

Once a bean object is loaded into the page, you can use two other action elements to manipulate it.

<jsp:setProperty name="obj" property="prop_name" value="prop_value"/>
<jsp:getProperty name="obj" property="prop_name"/>

The "setProperty" action will set a new value to the specified property of the specified bean object. The "getProperty" action will get the current value of the specified property of the specified bean object. This value will be converted into a string

Once a bean object is loaded into the page, it can be used in other scripting elements in the same JSP page.

Here is my first JavaBean, CacheBean.java:

/**
 * CacheBean.java
 * Copyright (c) 2002 by Dr. Herong Yang. All rights reserved.
 */
public class CacheBean {
  private String text = "null";
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
  public String getInfo() {
    return "My JavaBean - Version 1.00"
  }
}

Here is a simple JSP page to show you how to use JavaBean, UseBean.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- UseBean.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
<html><body>
<jsp:directive.page import="CacheBean"/>
<jsp:useBean id="b" class="CacheBean"/>
<jsp:setProperty name="b" property="text" value="Hello world!"/>
Property from my Bean: 
<jsp:getProperty name="b" property="text"/>
<br/>
Info from my Bean: 
<jsp:expression>b.getInfo()</jsp:expression>
</body></html>
</jsp:root>

Then I compiled CacheBean.java with JDK 1.3.1, and copied CacheBean.class to \local\jakarta-tomcat-4.1.18\webapps\root\web-inf\classes. Here is what I got by requesting UseBean.jsp from IE:

Property from my Bean: Hello world!
Info from my Bean: My JavaBean - Version 1.00

Note that CacheBean class needs to be imported into the JSP page. The class file needs to be copied to the .\web-inf\classes directory.

(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