Using Java Objects as JavaBeans

This section provides a tutorial example on how to use a regular Java object as JavaBean using the 'setProperty' action elements. This requires the Java object to be added to 'pageContext' attribute collection.

Now we know that a JavaBean is just a normal Java object with some specially named methods, and stored in pageContext's attribute collection. We can use the useBean to create a JavaBean and use it any way we wanted.

The next question is: can we create any object and put it into pageContext's attribute collection, and use setProperty elements? The answer is yes. Here is my sample JSP page to show you this:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="" version="2.1">
<!-- ObjectAsBean.jspx
 - Copyright (c) 2006 All Rights Reserved.
< contentType="text/html"/>
   herong.DemoBean b = new herong.DemoBean();
   pageContext.setAttribute("b", b, PageContext.PAGE_SCOPE);

<jsp:setProperty name="b" property="author" value="Unknown"/>
Line 21: author = <jsp:expression>b.getAuthor()</jsp:expression><br/>

Line 22: total = <jsp:expression>b.getTotal()</jsp:expression><br/>

Line 23: size = <jsp:expression>b.getSize()</jsp:expression><br/>

Line 24: size =

   java.util.Date d = new java.util.Date();
   pageContext.setAttribute("d", d, PageContext.PAGE_SCOPE);

<jsp:setProperty name="d" property="time" value="1000000000000"/>
Line 25: time = <jsp:expression>d.getTime()</jsp:expression><br/>


Open this JSP page with a Web browser, you will get:

Line 21: author = Unknown
Line 22: total = 1
Line 23: size = int: 15
Line 24: size = int: 15
Line 25: time = 1000000000000

Note that:

Notice that, I am not using "getProperty" action elements in this example. Why? Because it does work not in Tomcat. See next tutorial for more details.

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