Using JavaBean without Import Element Error

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.

So what can we do about the compilation error presented in the previous section? Can we remove the import statement? The answer is no. You can try by running the following modified JSP page, SecondBeanModified.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- SecondBeanModified.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:useBean id="b" class="SecondBean"/>
<jsp:setProperty name="b" property="text" value="Hello world!"/>
Property from my Bean:
<jsp:getProperty name="b" property="text"/>
<br/>
Info from my Bean:
<jsp:expression>b.getInfo()</jsp:expression>
</body></html>
</jsp:root>

When visiting SecondBeanModified.jspx with a Web browser, the "import" statement error is gone. But the other error, "SecondBean cannot be resolved to a type", is still there:

HTTP Status 500 - Unable to compile class for JSP:

type: Exception report

message: Unable to compile class for JSP:

description: The server encountered an internal error that prevented
it from fulfilling this request.

exception:
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 8 in the jsp file: /SecondBeanModified.jspx
SecondBean cannot be resolved to a type
5: -->
6: <jsp:directive.page contentType="text/html"/>
7: <html><body>
8: <jsp:useBean id="b" class="SecondBean"/>
9: <jsp:setProperty name="b" property="text" value="Hello world!"/>
10: Property from my Bean:
11: <jsp:getProperty name="b" property="text"/>
...

The actual cause of this error is not the Java compiler. It is the Tomcat compilation environment. Because you can compile the converted class, SecondBeanModified_jspx.java, correctly in a command window with same class paths used by the Tomcat environment.

The only option left is to declare JavaBean classes in named packages when creating them. The next tutorial will give you an example.

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