Accessing Native methods from a Java Applet by Sanket Bakshi, s.bakshi@ebsolutech.com |
|
Previous
section: Web Page to Applet Communication
o The MAYSCRIPT Tag
o How Applet to
Java-Script communication works in Java Plug-in
o JSObject Support
in Java Plug-in
The MAYSCRIPT
tag for the Applet was introduced with Netscape Navigator 3. It allows a Java
applet to communicate and interact with the document. If
an applet tries to access JavaScript without this attribute, the browser will
generate an error. There is no value assigned to this attribute, as the
presence of the MAYSCRIPT attribute activates the on state
The
Browser versions that support the MAYSCRIPT attribute are –
o
Internet Explorer 4.0 and above
o Netscape
Navigator 3.0 and above
The
Applet to JavaScript communication needs the MAYSCRIPT
attribute to be activated before it can take place.
Java
applets may need to perform Java to JavaScript communication to access the
Document Object Model (DOM) or to call JavaScript functions on an HTML page.
Internet Explorer and Navigator allow communication between Java to JavaScript
through the Java wrapper class netscape.javascript.JSObject.
Because
of differences in DOM implementations between browsers, Java Plug-in 1.2.2
provides different degrees of support for JSObject in Internet Explorer and
Netscape Navigator.
In
general, the applets can access the JavaScript objects in the following way –
import netscape.javascript.*;
import java.applet.*;
class MyApplet extends Applet {
public void init() {
JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject)
win.getMember("document");
JSObject loc = (JSObject)
doc.getMember("location");
String s = (String) loc.getMember("href"); // document.location.href
win.call("func", null); // Call func() in HTML
page
}
}
The getWindow() function will return a JSObject that
represents the Window object in the JavaScript. This function takes an applet as
a parameter. Once the Window object is got, the DOM of the HTML page can be
accessed with following functions –
public
Object call(String methodName, Object args[])
public
Object eval(String s)
public
Object getMember(String name)
public
Object getSlot(int index)
public
void removeMember(String name)
public
void setMember(String name, Object value)
public
void setSlot(int index, Object value)
public
String toString()
The
implementations of getSlot(), setSlot(), removeMember() and toString() are browser
dependant.
The Java
Plug-in by default does not enable the MAYSCRIPT tag. For the Java Plug-in to
support the JSObject, the MAYSCRIPT tag should be enabled in the <OBJECT>
and <EMBED> tags as follows –
APPLET
TAG
<APPLET
code="XYZApp.class" codebase="html/"
align="baseline"
width="200" height="200" MAYSCRIPT>
<PARAM
NAME="car" VALUE="Mercedes">
</APPLET>
NEW
OBJECT TAG
<OBJECT
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="200"
height="200" align="baseline"
codebase="http://java.sun.com/products/plugin/1.2/jinstall-12-win.cab#Version=1,2,0,0">
<PARAM
NAME="code" VALUE="XYZApp.class">
<PARAM
NAME="codebase" VALUE="html/">
<PARAM
NAME= "type"VALUE="application/x-java-applet;version=1.2">
<PARAM NAME="MAYSCRIPT"
VALUE="true">
<PARAM
NAME="car" VALUE="Mercedes">
</OBJECT>
NEW
EMBED TAG
<EMBEDtype="application/x-java-applet;version=1.2" width="200"
height="200"
align="baseline" code="XYZApp.class"
codebase="html/"
car="Mercedes" MAYSCRIPT=true
pluginspage="http://java.sun.com/products/plugin/1.2/plugin-install.html">
</EMBED>
Next section: Solution
– Invoking the JNI calls from Applets
Table of Contents
©
2001 – EBSolute Technologies