JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

Tomcat 4.1.18 with JDK 1.4.1

Part:   1  2 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

You will get the following compilation error saying CacheBean is not resolvable:

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

An error occurred at line: 7 in the jsp file: /jsp/UseBeanModified.jsp

Generated servlet error:
    [javac] Compiling 1 source file

D:\local\jakarta-tomcat-4.1.18\work\Standalone\localhost
   \_\jsp\UseBeanModified_jsp.java:43: cannot resolve symbol
symbol  : class CacheBean 
location: class org.apache.jsp.UseBeanModified_jsp
      CacheBean b = null;
      ^

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

The only option left to use a JavaBean with Tomcat 4.1.18 and JDK 1.4.1 is to create your JavaBean in a named package. The next section will give you an example.

JavaBean in a Named Package - TempratureConvertorBean.java

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 classe defined in a package named as "herong":

/**
 * TempratureConvertorBean.java
 * Copyright (c) 2003 by Dr. Herong Yang. 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".

>\local\j2sdk1.4.1\bin\java TempratureConvertorBean.java
>copy TempratureConvertorBean.class 
   \local\jakarta-tomcat-4.1.18\webapps\root\web-inf\classes\herong

Now we are ready to test this JavaBean with an JSP page, TempratureConvertor.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- TempratureConvertor.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<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.

Conclusions:

  • Using JavaBean classes in unnamed packages with Tomcat 4.1.18 and JDK 1.3.1 requires import directive elements in the JSP pages.
  • There is no way to use JavaBean classes in unnamed packages with Tomcat 4.1.18 and JDK 1.4.1.
  • There is no problem using JavaBean classes in named packages with Tomcat 4.1.18 and JDK 1.4.1.

Part:   1  2 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - Tomcat 4.1.18 with JDK 1.4.1