Given by Marek Podgorny,Nancy McCracken at CPS640 Internet and Multimedia Technologies on April 98. Foils prepared 2 October 98
Outside Index
Summary of Material
This describes LiveConnect which is Client Side Integration Technology |
We describe Java to JavaScript Connectivity and Vice Versa |
We use wv plug-in as an example of linking plug-ins with Java and JavaScript |
wv does wavelet decompression |
Outside Index Summary of Material
CPS640 Network and Multimedia Technologies |
Spring 1998 |
Marek Podgorny |
Nancy McCracken |
Java
|
Java applets
|
JavaScript
|
Netscape plug-ins
|
Netscape LiveConnect
|
Native code - any language linkable with C |
Java allows one to:
|
LiveConnect allows one to:
|
With some ingenuity, one can:
|
Browser entities communication capabilities |
Browser entities cross-awareness |
JavaScript can directly call any public method defined in the applet and access any public data |
JavaScript accesses the applet via the document object |
JavaScript can be used, for instance, to debug applet's code by providing interactive interface to the applet's methods |
import java.applet.*; |
import java.awt.Graphics; |
public class Hello extends Applet { |
String str = "Hello, World"; |
int x = 25; |
int y = 25; |
public void paint ( Graphics g){ |
g.drawString ( str, x, y ); |
} |
public void setString ( String newstr ){ |
str = newstr; |
repaint(); |
} |
public String getString() { |
return str; |
} |
<html> |
<applet name=hello |
code=Hello.class |
width=200 height=100> |
</applet> |
<form > |
<input name="In" type=text size=35> |
<input type=button value="Set" |
onclick="document.hello.setString(form.In.value)"> |
<br> |
<input name="Out" type=text size=35> |
<input type=button value="Get" |
onclick="form.Out.value=document.hello.getString()"> |
</form> |
</html> |
JavaScript defines hierarchy of objects on HTML page. |
These objects are accessible from Java via LiveConnect mechanisms implemented in netscape.javascript. JSObject class.
|
JavaScript is aware of Java objects and can call their methods directly.
|
import java.applet.*; import java.awt.*; |
import netscape.javascript.JSObject; |
public class SayThanks extends Applet { |
JSObject win; |
String str = "Click for an important message"; |
int x = 25; int y = 25; |
public void paint ( Graphics g ) { |
g.drawString ( str, x, y );} |
public void init() |
{ win = JSObject.getWindow(this);} |
public boolean mouseUp(Event e, int x, int y) |
{ JSObject document = |
(JSObject) win.getMember ( "document" ); |
Object args[] = new Object[1]; |
String title = |
(String) document.getMember ( "title" ); |
args[0] = "Thanks for visiting " + title; |
win.call ( "alert", args ); |
return true; |
} |
<html> |
<title> My Web Page </title> |
<applet code=SayThanks.class |
width=200 height=100 |
mayscript=true > |
</applet> |
</html> |
The Java language uses native method modifier to declare a method implemented in another language. |
The native signature of the method as well as wrapper functions are machine generated using program javah. |
The class using native method needs to load the native library. |
Example
|
Java interface to plug-in is necessary! |
Netscape provides netscape.plugin.Plugin class which is the Java representation of the native plug-in. |
Specifics for particular plug-in subclass of java.plugin.Plugin must be created and installed together with plug-in itself (NPX_PLUGIN_PATH). |
LiveConnect mechanism automatically instantiates Java class representing plug-in. |
<head> <script language="JavaScript"> |
function doNothing() {} // A dummy function |
function playSound ( soundName ) |
{ plugin = document.plugins[soundName]; |
window.status = soundName |
if ( plugin != null ) |
{ plugin.StopAll(); |
plugin.play(false); |
setTimeout('plugin.stop()', 2000); } } |
</script> |
<title> Sound Sampler </title></head> <body> |
<h2> Point to an instrument to hear how it sounds</h2> |
<embed src="keyboard.aiff" name="keyboard" |
hidden=true volume= 100% autostart=false> .... |
<a href="doNothing()" |
onMouseOver="playSound('keyboard'); return true;"> |
<img src="keyboard.gif" border=0> </a> .... |
</body></html> |
Communication between this object and the plug-in is done the same way as in general case of Java-native code communication, except for:
|
Communication between applet and plug-in is done via JavaScript. |
Both java.applet.Applet and netscape.plugin.Plugin class can access JavaScript hierarchy and learn about each other.
|
libWvDecoder.so |
WvDecoder.class |
HTTP server |
Web browser |
NPX_PLUGIN_PATH |
(LiveConnect capable) |
browser finds <embed> tag in HTML page
|
libWvDecoder.so calls use_WvDecoder(NPN_GetJavaEnv())
|
browser finds <applet> tag
|
viewer calls Java Script: JSObject.getWindow(this).
|
Communication is established:
|
LiveConnect enables creation of externally callable interface to a plug-in
|