Using Beans with Tomcat 4

It appears that Tomcat 4 expects beans to be packaged. Tomcat 4 complains that it can't find beans that belong to the default package and are deployed in the myapp/WEB-INF/classes directory.

Example

Assume a web application called web0 will contain a page budget.jsp, which will use instances of the class BudgetBean.

Step 1

To make this work, we must create a package for BudgetBean. To make things easier, we decide that a single package called beans will contain all of web0's beans and servlets.

Here's BudgetBean.java:

package beans;
public class BudgetBean implements java.io.serializable { ... }

Note 1: No code can precede the package declaration.

Note 2: BudgetBean must be declared public.

If our development area is called c:\myprojects\web0, then we must create a subdirectory of myprojects\web0\src called beans. This is where BudgetBean.java is placed. Here's the complete path name of BudgetBean.java:

c:\myprojects\web0\src\beans\BudgetBean.java

Step 2

The usebean action must use the fully qualified name of the BudgetBean class. It's probably a good idea to use the page directive to import the beans package.

Here's budget.jsp:

<%@ page import = "beans.*" %>
<jsp:useBean
   id = "budget" scope = "request" class = "beans.BudgetBean" />
<html>
...
</html>

Step 3

Build web0. If your test area is c:\tomcat\webapps\web0, then ant should have created the file:

c:\tomcat\webapps\web0\WEB-INF\classes\beans\BudgetBean.class

Step 4 (CS174 students only)

If web0 works on your test machine, then use ftp (in binary mode) to copy the beans directory to

$tomcat/web0/WEB-INF/classes

Of course you will be copying to webN, for some N > 0.