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,

 * Copyright (c) 2012,, 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="" version="2.1">
<!-- SecondBean.jspx
 - Copyright (c) 2012,, All Rights Reserved.
< contentType="text/html"/>
< 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"/>
Info from my Bean: 

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


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

C:\>copy SecondBean.jspx \local\apache-tomcat-7.0.32\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.

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: < contentType="text/html"/>
7: <html><body>
8: < 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 ( import="SecondBean") derective element, which is converted as the (import SecondBean;) Java statement. Since JDK 1.4, "import" statement is not allowed for classes in unnamed packages.

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