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="" version="2.1">
<!-- BeanAsObject.jspx
 - Copyright (c) 2012,, All Rights Reserved.
< contentType="text/html"/>
<jsp:useBean id="b" class="herong.DemoBean"/>
<jsp:setProperty name="b" property="author" value="Someone"/>
Line 11: author = 

Line 12: total = 
<jsp:getProperty name="b" property="total"/><br/>

Line 13: size = 
<jsp:getProperty name="b" property="size"/><br/>
Line 14: size = 

   Object o = pageContext.findAttribute("b");
   String s = ((herong.DemoBean)o).getSize();
   out.println("Line 15: size = "+s);

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:

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


 PDF Printing Version