Sun
ONE Active Server Pages Product Home Page Developer Site Version
 

ContentsPreviousNextIndex



Accessing a Java Class via Chili!Beans


Sun ONE ASP 4.0 simplifies the creation of Java objects by providing a new Chili!Beans object method that allows you to instantiate Java classes with a single line of code. The new method is NewJavaObject. With this method, instantiating the Chili!Beans object and then calling the Construct method are combined into one step, so:

Set y = NewJavaObject("foo.bar")

has the same effect as

Set z = Server.CreateObject("Chili.Beans")
Set y = z.Construct("foo.bar")

While use of NewJavaObject is recommended, you can also use the Chili!Beans object Construct method to create instances of Chili!Beans. With the Construct method, the first argument is the fully qualified name of the class to be instantiated, and the remaining arguments are the arguments to be passed to the desired constructor for that class. For example, if the package Database contains Table.class, the following script will create a Table object:

Set factory = Server.CreateObject("Chili.Beans")
Set table = factory.Construct "Database/Table", "Employees", CLng(100)

This will create an object named Table, using the constructor whose signature is:

constructor(String, Int)

If the class name cannot be found on the CLASSPATH, or if there is no public constructor whose signature matches the arguments passed to Construct, a run-time error occurs in the script.

To use a Java class with Sun ONE ASP, the .class file must exist in a directory that is listed in the Java CLASSPATH environment variable, or it must be registered with Sun ONE ASP as described in Registering a Java Class as a COM Component on Linux and UNIX in this section. A classpath can also be specified via the Sun ONE ASP Administration Console, as described in Enabling Chili!Beans.

Caution icon Caution Static variables in a Java class are shared by every user who creates an instance of that class. This can create unexpected behavior because conflicts can occur when two instances of a class (even in different scripts) attempt to change the value.


ContentsPreviousNextIndex