Chapter 14 Java packages for LiveConnectThe LiveConnect facility allows your JavaScript application to work with Java objects and for those Java objects to work with JavaScript objects. LiveConnect provides two Java applet API packages for communicating with JavaScript. These packages are netscape.javascript and netscape.plugin. The netscape.javascript applet package is available both on the client and on the server and has the following classes:The netscape.plugin applet API package can be used only on the client. It has the following class:The following sections describe these classes and list their constructors and methods.
netscape.javascript.JSObjectThe public final class JSObject extends Object .java.lang.Object | +----netscape.javascript.JSObject JSObject allows Java to manipulate objects that are defined in JavaScript. Values passed from Java to JavaScript are converted as follows:JSObject is converted to the original JavaScript object.
- Any other Java object is converted to a JavaScript wrapper, which can be used to access methods and fields of the Java object. Converting this wrapper to a string will call the
toString method on the original object, converting to a number will call the floatValue method if possible and fail otherwise. Converting to a boolean will try to call the booleanValue method in the same way.
- Java arrays are wrapped with a JavaScript object that understands
array.length and array[index] .
- A Java boolean is converted to a JavaScript boolean.
- Java byte, char, short, int, long, float, and double are converted to JavaScript numbers.
Values passed from JavaScript to Java are converted as follows:- Objects that are wrappers around Java objects are unwrapped.
- Other objects are wrapped with a
JSObject .
- Strings, numbers, and booleans are converted to String, Float, and Boolean objects respectively.
This means that all JavaScript values show up as some kind of java.lang.Object in Java. In order to make much use of them, you will have to cast them to the appropriate subclass of Object , as shown in the following examples:(String) window.getMember("name") (JSObject) window.getMember("document") Note If you call a Java method from JavaScript, this conversion happens automatically--you can pass in "int" argument and it works. Methods and static methodsThe netscape.javascript.JSObject class has the following methods:Table 14.1 Methods for the JSObject class Method | Description |
---|
call
| Calls a JavaScript method | eval
| Evaluates a JavaScript expression | getMember
| Retrieves a named member of a JavaScript object | getSlot
| Retrieves an indexed member of a JavaScript object | removeMember
| Removes a named member of a JavaScript object | setMember
| Sets a named member of a JavaScript object | setSlot
| Sets an indexed member of a JavaScript object | toString
| Converts a JSObject to a string | The netscape.javascript.JSObject class has the following static methods:Table 14.2 Static methods for the JSObject class Method | Description |
---|
getWindow
| Gets a JSObject for the window containing the given applet | The following sections show the declaration and usage of these methods.callMethod. Calls a JavaScript method. Equivalent to "this.methodName(args[0], args[1], ...) " in JavaScript. Declarationpublic Object call(String methodName, Object args[]) evalMethod. Evaluates a JavaScript expression. The expression is a string of JavaScript source code which will be evaluated in the context given by "this". Declarationpublic Object eval(String s) getMemberMethod. Retrieves a named member of a JavaScript object. Equivalent to "this.name " in JavaScript. Declarationpublic Object getMember(String name) getSlotMethod. Retrieves an indexed member of a JavaScript object. Equivalent to "this[index] " in JavaScript. Declarationpublic Object getSlot(int index) getWindowStatic method. Returns a JSObject for the window containing the given applet. This method is available only on the client. Declarationpublic static JSObject getWindow(Applet applet) removeMemberMethod. Removes a named member of a JavaScript object. Declarationpublic void removeMember(String name) setMemberMethod. Sets a named member of a JavaScript object. Equivalent to "this.name = value " in JavaScript. Declarationpublic void setMember(String name, Object value) setSlotMethod. Sets an indexed member of a JavaScript object. Equivalent to "this[index] = value " in JavaScript. Declarationpublic void setSlot(int index, Object value) toStringMethod. Converts a JSObject to a String .Overrides: toString in class Object Declarationpublic String toString() |