This section describes the JavaScript communication of JBK Plugin.
JBK Plugin supports the JavaScript communication with Internet Explorer.
This section describes the following items:
To call JavaScript from Java applet, it is necessary that Java communicates with JavaScript. JBK Plugin enables Java to communication with JavaScript using netscape.javascript.JSObject object, which is instance of Java wrapper class, in Internet Explorer.
Class:
netscape.javascript.JSObject
(extends java.lang.Object)
Methods:
getWindow
Gets the JSObject of the window (Window object of JavaScript) containing the given applet. This method takes only java.applet.Applet object as the parameter.
public static JSObject getWindow(java.applet.Applet a) |
call
Calls the JavaScript method. Equivalent to "this.methodName(args[0], args[1], ...)" in JavaScript.
public Object call(String methodName, Object args[]) |
eval
Evaluates the JavaScript expression.
public Object eval(String s) |
getMember
Retrieves the named member of the JavaScript object.
public Object getMember(String name) |
getSlot
Retrieves the indexed member of the JavaScript object.
public Object getSlot(int index) |
setMember
Sets the named member of a JavaScript object.
public void setMember(String name, Object value) |
setSlot
Sets the indexed member of the JavaScript object.
public void setSlot(int index, Object value) |
toString
Converts the JSObject to a String.
public String toString() |
removeMember
Not supported.
public void removeMember(String name) |
In the specification of Java packages for LiveConnect, removeMember method exists, but is not supported in Internet Explorer.
Supported objects by JSObject
The following JavaScript objects can be accessed with JBK Plugin via JSObject.
JBK Plugin can access the following JavaScript objects via JSObject:
Anchor/Applet/Array/Boolean/Button/Checkbox/Date/document/Element/Fileupload/Form/ |
arguments.length, arguments[], and caller properties of a Function object cannot be used.
If the applet calls a JavaScript method whose parameters are instance of JavaScript object, e.g. concat() method of an Array object, use eval() method of JSObject to process exactly.
How to use JSObject
The following is the example to call JavaScript from the applet using JSObject:
import netscape.javascript.*; import java.applet.*; public class Applet1 extends Applet { public void init() { JSObject win = JSObject.getWindow(this); try{ // call alert method of JavaScript Window object win.call("alert", new Object[]{"Test"}); // set the status property of JavaScript Window object win.eval("this.status=\"Hello World\""); /* document.forms[0].elements[0] is <INPUT TYPE=button>. */ // refer the button, and set the value property to "Button". JSObject doc = (JSObject) win.getMember("document"); JSObject forms = (JSObject) doc.getMember("forms"); JSObject aform = (JSObject) forms.getMember("0"); JSObject elements = (JSObject) aform.getMember("elements"); JSObject script_button = (JSObject) elements.getMember("0"); script_button.setMember("value", "Button"); // exception handling for JSObject }catch(JSException e){ e.printStackTrace(); } } } |
The following shows an example of HTML in which a form is called by the above applet.
[HTML description]
<HTML> <HEAD> <SCRIPT src="JBKTag.js" type="text/javascript" language="JavaScript"> </SCRIPT> </HEAD> <BODY> <FORM> |
[JavaScript code(JBKTag.js)]
function showapplet() { document.writeln('<OBJECT CLASSID="CLSID:BEA62964-C40B-11D1-AACA-00A0C9216A67" WIDTH=100 HEIGHT=100>'); document.writeln('<PARAM NAME="TYPE" VALUE="application/x-JBK-Plugin">'); document.writeln('<PARAM NAME="NAME" VALUE="sample">'); document.writeln('<PARAM NAME="CODE" VALUE="Applet1.class">'); document.writeln('<PARAM NAME="color" VALUE="blue">'); document.writeln('<PARAM NAME="useDefault" VALUE="true">'); document.writeln('</OBJECT>'); } |
The class file for the JSObject class and JSException class is stored in the jar file for the development of the JBK plugin ("classes\jbkstd.jar" in the JBK installation folder). When compiling the class of an applet that uses the PluginAppletContext interface, check whether the jar file for the development of the JBK plugin is included in the classpath.
Note
Observe the following notes derived from Internet Explorer limitations when using JavaScript Communication in Internet Explorer:
The user cannot use setMember method to set the properties of objects excluding window and document objects.
The user cannot use eval emthod excluding window objects.
Because of the security restrictions of Windows XP Service Pack 2, you must use absolute path instead of relative path to designate local files under the following conditions.
When opening a local file by using open method of the window object.
When setting a local file by using location property of the window object.
You cannot use getMember method to get the properties of some objects.
The type of the return value of the following methods of the Date object is Double in Internet Explorer in Netscape though it is Integer.
getYear
getMonth
getDate
getHours
getMinutes
getSeconds
getDay
getTimezoneOffset
Information
JSObject also supports the array of JavaScript. To get an array from applet using JavaScript, please refer to the following code.
import netscape.javascript.*; import java.applet.*; public class Applet1 extends Applet { public void init() { JSObject win = JSObject.getWindow(this); /* Array is set as 'arrayname = new Array("0", "1", "2", ...)' */ // get JavaScript Array object JSObject array = (JSObject)win.getMember("arrayname"); // get a number of index (Integer)array.getMember("length"); // get the first member of the array (String)array.getMember("0"); } } |
JBK Plugin allows calling a public method of an applet from JavaScript.
If the calling public method is overloaded, it cannot be guaranteed which of the methods is called.
Setting for Calling Method
To call a method of the applet from JavaScript, put the following line in the jbkplugin.properties file:
jbk.plugin.sw.script.enable =<method calling-used-or-not> |
Specify true or false to <method calling-used-or-not>.
Calling applet method is enabled.
Calling applet method is disabled.
By default, the value of this property is set to false.
How to call a method
To call a method of an applet from JavaScript, Please refer the following code.
Example: Calling Hello() method of the applet when the button1 is pushed.
[HTML description]
<OBJECT classid="CLSID:BEA62964-C40B-11D1-AACA-00A0C9216A67" width="160" height="120"> <PARAM NAME="CODE" VALUE="Applet1.class"> <PARAM NAME="TYPE" VALUE="application/x-JBK-Plugin"> <PARAM NAME="ARCHIVE" VALUE="TestApplet.jar"> <PARAM NAME="useDefault" VALUE="true"> </OBJECT> <INPUT TYPE="button" NAME="button1" VALUE="Button" onClick="sample()"> <Script language="JavaScript" FOR="window"> <!--- function sample() { alert(document.applets[0].Hello("XXXX")); } //----> </Script> |
[Applet code]
import java.applet.*; public class Applet1 extends Applet{ public void init() { } // return the formatted String public String Hello(String name){ String retVal = new String("Hello "); retVal += name ;retVal += "!!" ;return (retVal); } } |
Type conversion from JavaScript to applet
When JavaScript calls a method of an applet, JavaScript sends the argument to the applet. JavaScript value is converted to Java value. The conversion specifications are as follows:
argument of applet's method | Type of the value sent from JavaScript | |
---|---|---|
Type of JavaScript | Results | |
Byte/byte | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
| |
Character/char | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
| |
Short/short | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
| |
Integer/int | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
| |
Long/long | String values |
|
Number values |
*If long value is beyond the limits of int value, the result of conversion becomes 0. | |
Boolean values |
| |
null value |
| |
Object |
| |
Boolean/boolean | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
| |
Float/float | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
| |
Double/double | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
| |
String | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
| |
JSObject | String values |
|
Number values |
| |
Boolean values |
| |
null value |
| |
Object |
*Any method of JSObject class cannot be used. |
Type conversion from the return value of applet method to JavaScript
When an applet returns the result of calling method to JavaScript, the return value is converted to JavaScript value. These conversion specifications are as follows:
Return value type of applet method | JavaScript value |
---|---|
Void | null value |
Byte/byte | Number value |
Character/char | Number value |
Short/short | Number value |
Integer/int | Number value |
Long/long | Number value, the range is from -2147483648 to 2147483647 |
Boolean/boolean | Boolean value true is true, false is false. |
Float/float | Number value |
Double/double | Number value |
String | String value |
JSObject | Object value |
This section describes how to access cookie information.
An applet can access cookie by using cookie property of JavaScript document object which is referred via JSObject.
Example
Gets the cookie information from the browser, and sends it to the server.
import java.applet.*; import java.net.*; import netscape.javascript.*; public class Applet1 extends Applet { public void init(){ JSObject win = JSObject.getWindow(this); // refer the cookie property of JavaScript document object JSObject doc = (JSObject)win.getMember("document"); String cookie = (String)doc.getMember("cookie"); try{ /* set the URL to send the cookie */ // get codebase of the applet URL url = getCodeBase(); String strurl = url.toString(); // set URL /* in this case, "/servlet/cookie" */ strurl = strurl + "/servlet/cookie"; url = new URL(strurl); // set the HTTP request property HttpURLConnection urlCon = (HttpURLConnection)url.openConnection(); urlCon.setRequestProperty("cookie", cookie); urlCon.disconnect(); /* Describe the procedure using HttpURLConnection */ }catch(Exception e){ e.printStackTrace(); } } } |