"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 fully qualified 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) 2002 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.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<!-- UseBean.jsp
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
<jsp:directive.page contentType="text/html"/>
<jsp:directive.page import="herong.CacheBean"/>
<!-- Must use fully qualified class name
<jsp:useBean id="b" class="CacheBean"/>
<jsp:useBean id="b" class="herong.CacheBean"/>
<jsp:setProperty name="b" property="text" value="Hello world!"/>
Property from my JavaBean:
<jsp:getProperty name="b" property="text"/>
Info from my JavaBean:

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

herong> javac herong\CacheBean.java

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

Does \local\tomcat\webapps\ROOT\WEB-INF\classes\herong
specify a file name or directory name on the target
(F = file, D = directory)? d
2 File(s) copied

herong> copy UseBean.jspx \local\tomcat\webapps\ROOT
        1 file(s) copied.

Visit http://localhost:8080/UseBean.jspx page. You should see:

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

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


 Full Version in PDF/EPUB