JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
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":
/* TempratureConvertorBean.java * Copyright (c) 2002 HerongYang.com. 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".
herong> javac herong\TempratureConvertorBean.java herong> xcopy herong \local\tomcat\webapps\root\WEB-INF\classes 1 file(s) copied.
Now we are ready to test this JavaBean with a JSP page, TempratureConvertor.jspx:
<?xml version="1.0"?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3"> <!-- TempratureConvertor.jspx - Copyright (c) 2006 HerongYang.com. All Rights Reserved. --> <jsp:directive.page contentType="text/html"/> <html><body> <jsp:useBean id="b" class="herong.TempratureConvertorBean"/> <jsp:expression>b.getInfo()</jsp:expression><br/> 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"/> <br/> </body></html> </jsp:root>
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.
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
►JavaBean Objects and "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
Overview of JSTL (JSP Standard Tag Libraries)
Multiple Tags Working Together
Using Tomcat on CentOS Systems
Connecting to SQL Server from Servlet