LiveConnect allows JavaScript and Java to cooperate through two
fairly separate and symmetrical systems. In JavaScript, the
JavaPackage, JavaClass, JavaObject, JavaArray, and JavaMethod
objects all allow JavaScript to read and write Java properties
and arrays and to invoke Java methods. In Java, the
netscape.javascript.JSObject class allows
Java programs to read and write properties of JavaScript objects
and elements of JavaScript arrays, to invoke JavaScript
functions, and evaluate strings of JavaScript code. The
following two subsections summarize these two halves of
LiveConnect.
- The JavaClass, JavaObject, JavaArray, and
JavaMethod objects allow transparent communication between
JavaScript and Java--they handle data conversion and
all the tricky behind-the-scenes work.
- The data conversions performed when JavaScript
reads and writes Java values are illustrated in Figure 19-2 and Figure 19-3.
- Most Java objects are converted to JavaScript
JavaObject objects. JavaObjects behave differently than
other JavaScript objects, and need to be handled with care.
In particular, JavaObjects are converted to numeric, Boolean
and string values differently than other JavaScript types
are.
- You can use the JavaPackage objects referred to
by the Window properties Packages,
java, sun, and
netscape to obtain a JavaClass object for
any of the standard classes built in to Navigator. The
JavaClass object allows you to read and write static
properties and invoke static methods of a class.
- You can use the new operator
on a JavaClass object to create a new Java object and a
JavaScript JavaObject wrapper for it. You can use this
JavaObject to read and write instance fields and invoke
instance methods.
- You can use the
getClass() function to obtain a
JavaClass object corresponding to the Java class of a
JavaObject object.
- You can "script" Java directly from JavaScript
simply by working with the predefined classes. But this
technique is limited--no significant user interaction
with a "scripted" Java program is possible.
- You can also use the
document.applets[] array and the
JavaObject objects it contains to interact with applets.
Manipulating the fields and methods of a custom-written
applet allows a richer set of possibilities than simply
scripting with the basic Java classes.
- You can use the
document.embeds[] array and the
JavaObjects it contains to interact with the plug-ins that
are displaying embedded data in the document. You can
control plug-ins through vendor-specific Java APIs.
- The netscape.javascript.JSObject
class is the Java equivalent of the JavaScript JavaObject
class. It handles data conversion and all the
behind-the-scenes work to allow Java code to communicate
with JavaScript.
- The data conversions performed when Java reads and writes
JavaScript data are illustrated in
Figure 19-5 and
Figure 19-6.
- The getMember() and
getSlot() methods of a JSObject allow
Java to read JavaScript object properties and array elements.
- The setMember() and
setSlot() methods allow Java to set the
value of JavaScript object properties and array elements.
- The call() method of a JSObject allows
Java to invoke JavaScript functions.
- The eval() method of a JSObject allows
Java to pass an arbitrary string of JavaScript code to the
JavaScript interpreter for execution. This method is often
easier to use than the other JSObject methods.
- An applet that uses the JSObject class must
import it with an import statement. To
compile the applet, the CLASSPATH
environment variable must be set to include the Java classes
supplied by Netscape.
- In order to interact with JavaScript, an applet must be
embedded in an HTML document with an
<APPLET> tag that includes the
MAYSCRIPT attribute.