Creating JavaBean Classes in Named Packages

This section describes the compilation error on a JSP page that uses a JavaBean without the 'page import' directive element to import the JavaBean class declared without package name.

Based on what we have learned from the previous sections, JavaBean classes must be declared in named packages so that they can be used in JSP pages.

Creating JavaBean classes in named packages is very simple, just add a package declaration statement at the beginning of the class. The following example shows you a JavaBean class defined in a package named called "herong":

 * Copyright (c) 2012,, All Rights Reserved.
package herong;
public class TempratureConvertorBean {
  private double celsius = 0.0;
  private double fahrenheit = 32.0;
  public double getCelsius() {
    return celsius;
  public void setCelsius(double c) {
    celsius = c;
    fahrenheit = 1.8*c + 32.0;
  public double getFahrenheit() {
    return fahrenheit;
  public void setFahrenheit(double f) {
    fahrenheit = f;
    celsius = (f-32.0)/1.8;
  public String getInfo() {
    return new String("My TempraturConvertorBean - Version 1.00");

Compile this source code, and copy the class file to the Tomcat class path. Remember to store the class file under a sub directory named as "herong".


C:\>copy herong\TempratureConvertorBean.class 
        1 file(s) copied.

Now we are ready to test this JavaBean with an JSP page, TempratureConvertor.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="" version="2.1">
<!-- TempratureConvertor.jspx
 - Copyright (c) 2012,, All Rights Reserved.
< contentType="text/html"/>
<jsp:useBean id="b" class="herong.TempratureConvertorBean"/>
Setting property "fahrenheit" to 70.0.<br/>
<jsp:setProperty name="b" property="fahrenheit" value="70.0"/>
Getting property "celsius" back: 
<jsp:getProperty name="b" property="celsius"/>

Open this JSP page with IE, you will get:

My TempraturConvertorBean - Version 1.00
Setting property "fahrenheit" to 70.0.
Getting property "celsius" back: 21.11111111111111

It works! Note that there is no need to use the import statement, if you use the fully qualified class name in the jsp:useBean action element.

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