Using JavaBean Objects in Scripting Elements

This section provides a tutorial example on how to use JavaBean objects in scripting elements. A JavaBean is really just Java class object, which can be used in any scripting elements.

As I mentioned in the previous section, JavaBean is just a normal Java object with some special methods. Once a JavaBean is created, we should be able to use it as Java object in any scripting elements.

Here is a sample page to show you how to use a JavaBean as a Java object. It is using the same JavaBean class, herong.DemoBean, as provided in the previous section.

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<!-- BeanAsObject.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:useBean id="b" class="herong.DemoBean"/>
<jsp:setProperty name="b" property="author" value="Someone"/>
Line 11: author =
<jsp:expression>b.getAuthor()</jsp:expression><br/>

<jsp:scriptlet><![CDATA[b.setTotal(10);]]></jsp:scriptlet>
Line 12: total =
<jsp:getProperty name="b" property="total"/><br/>

<jsp:scriptlet><![CDATA[b.setSize(15);]]></jsp:scriptlet>
Line 13: size =
<jsp:getProperty name="b" property="size"/><br/>
Line 14: size =
<jsp:scriptlet><![CDATA[out.println(b.getSize());]]></jsp:scriptlet>
<br/>

<jsp:scriptlet><![CDATA[
   Object o = pageContext.findAttribute("b");
   String s = ((herong.DemoBean)o).getSize();
   out.println("Line 15: size = "+s);
]]></jsp:scriptlet><br/>
</body></html>
</jsp:root>

Open this JSP page with IE, you will get:

Line 11: author = Someone
Line 12: total = int: 10
Line 13: size = int: 15
Line 14: size = int: 15
Line 15: size = int: 15

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

 References

 Full Version in PDF/EPUB