Importing Unnamed Package Class Error

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

If you are writing simple JavaBean classes for testing purpose and forgetting to declare them in a named package, you will get Tomcat compilation errors when those JavaBean classes in JSP pages.

To demonstrate the Tomcat error, let's see this JavaBean class example, SecondBean.java:

/* SecondBean.java
 * Copyright (c) 2002 HerongYang.com. All Rights Reserved.
 */
public class SecondBean {
  private String text = "null";
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
  public String getInfo() {
    return "My JavaBean - Version 1.00";
  }
}

Here is the JSP page, SecondBean.jspx, that uses my SecondBean class:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- SecondBean.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:directive.page import="SecondBean"/>
<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>

To deploy the JavaBean class and the JSP page to the Tomcat server, run these commands in a command window:

herong> javac SecondBean.java

herong> copy SecondBean.class \local\tomcat\webapps\root\WEB-INF\classes
        1 file(s) copied.

herong> copy SecondBean.jspx \local\tomcat\webapps\root\
        1 file(s) copied.

When visiting SecondBean.jspx with a Web browser, I do receive this compilation error:

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: 14 in the generated java file
The import SecondBean cannot be resolved

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

...

As you can see from the error message, Tomcat server is not able to compile the Servlet class converted from the JSP page. The root cause is the (directive.page import="SecondBean") directive element, which is converted as the (import SecondBean;) Java statement. Since JDK 1.4, "import" statement is not allowed for classes in unnamed packages.

The solution is to modify the SecondBean class by put it in a package like "herong.SecondBean".

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