JSP Tutorials - Herong's Tutorial Examples - 5.10, by Dr. Herong Yang
Outdated: Compilation Errors with JDK 1.4
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.
To demonstrate the Tomcat error, let's review my FirstBean.java class again:
/** * FirstBean.java * Copyright (c) HerongYang.com. All Rights Reserved. */ public class FirstBean { 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, FirstBean.jspx, that uses my FirstBean class:
<?xml version="1.0"?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"> <!-- FirstBean.jspx - Copyright (c) HerongYang.com. All Rights Reserved. --> <jsp:directive.page contentType="text/html"/> <html><body> <jsp:directive.page import="FirstBean"/> <jsp:useBean id="b" class="FirstBean"/> <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:
>\local\j2sdk1.4.1\bin\javac FirstBean.java >copy FirstBean.class \local\jakarta-tomcat-4.1.18\webapps\ROOT\WEB-INF\classes >copy FirstBean.jspx \local\jakarta-tomcat-4.1.18\webapps\ROOT\
When visiting FirstBean.jspx with a Web browser, I do receive this compilation error:
org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: 9 in the jsp file: /FirstBean.jspx Generated servlet error: [javac] Compiling 1 source file C:\local\jakarta-tomcat-4.1.18\work\Standalone\localhost \_\FirstBean_jsp.java:8: '.' expected import FirstBean;
After reading the JDK 1.4 documentation, I found the root cause of the error. My FirstBean class was declared without any package name. So it is considered as in the unnamed package. JDK 1.4 added a new rule that does not allow an import statement to be used on classes in unnamed packages.
See next tutorial on suggestions to resolve this compilation error.
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
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
Developing Web Applications with Servlet
Outdated: Installing GlassFish JSTL 1.2 on Tomcat
Outdated: Downloading and Installing Tomcat 7
Outdated: Installing Tomcat 5.5.7
Outdated: Installing Tomcat 4.1.18
Outdated: Java Class Converted by Tomcat 4.1.18
Outdated: Hijacking Servlet Converted from JSP
Outdated: Using Perl LWP::Debug Module to Debug
Outdated: Installing JSTL 1.0 Apache Implementation
Outdated: Upgrade JDK 1.3 to JDK 1.4 on Tomcat 4.1
►Outdated: Compilation Errors with JDK 1.4