"jsp:useBean" Action Elements

This section provides a tutorial example on how to use the 'jsp:useBean' action element to initiate a JavaBean object into a JSP page. 'jsp:setProperty' and 'jsp:getProperty' action elements are used save and retrieve values to and from JavaBean objects.

useBean is a JSP action element that initiates a JavaBean object into the JSP page using the following syntax:

<jsp:useBean id="o_name" class="c_name"/>

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

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

<jsp:setProperty name="o_name" property="p_name" value="p_value"/>
<jsp:getProperty name="o_name" property="p_name"/>

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

The loaded JavaBean object can also be used in other scripting elements in the same JSP page.

Here is an example JavaBean, CacheBean.java, that will be used in out test JSP pages:

/**
 * CacheBean.java
 * Copyright (c) 2012, HerongYang.com, All Rights Reserved.
 */
package herong; 
import java.io.*;
public class CacheBean implements Serializable {
  private String text = "null";
  public CacheBean() {}
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
  public String getInfo() {
    return new String("CacheBean - 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="2.1">
<!-- UseBean.jsp
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:directive.page import="herong.CacheBean"/>
<jsp:useBean id="b" class="CacheBean"/>
<jsp:setProperty name="b" property="text" value="Hello world!"/>
Property from my JavaBean: 
<jsp:getProperty name="b" property="text"/>
<br/>
Info from my JavaBean: 
<jsp:expression>b.getInfo()</jsp:expression>
</body></html>
</jsp:root>

To test this JSP page, compile CacheBean.java with JDK 1.6 or higher, and deploy the JavaBean class and the JSP page to the Tomcat 7 server:

C:\>\Progra~1\java\jdk1.7.0_07\bin\javac herong\CacheBean.java
      
C:\>xcopy herong 
   \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes\herong
Does \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes\herong 
specify a file name or directory name on the target
(F = file, D = directory)? d
herong\CacheBean.class
herong\CacheBean.java
2 File(s) copied
        
C:\>copy UseBean.jspx \local\apache-tomcat-7.0.32\webapps\ROOT
        1 file(s) copied.

Visit UseBean.jspx page from IE. You should see:

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

Note that:

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 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

 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

 Outdated Tutorials

 References

 PDF Printing Version