Overview Overview 1) Introduction 2) Setting up the Environment 3) Java to JavaScript 4) JavaScript to Java Phil Denis 5) Java to Plug-ins Anju Tai 6) Conclusion 7) Question & Answer Setting Up the Environment Setting Up the Environment Introduction Introduction Enable Java and JavaScript in the browser LiveConnect: � � For Java to JavaScript communication • Technology developed by Netscape � 1) Add java40.jar to your CLASSPATH • Allows inter-communication between Java, JavaScript and plug-ins 2) Import netscape.javascript package • Communication on the client side 3) Grant Java permission to access JavaScript • First available in Navigator 3.0 <APPLET CODE=“MyApplet.class” WIDTH=… Microsoft followed in IE 4.0 � HEIGHT=… MAYSCRIPT > … </APPLET>
Setting Up the Environment Java to JavaScript Setting Up the Environment Java to JavaScript For JavaScript to Java communication 1) netscape.javascript package � • Methods called by JavaScript are public 2) Accessing JavaScript Functionality For Java Communication with plug-ins � 1) Add java40.jar to your CLASSPATH 2) Java plug-ins are compiled with the Plugin class 3) Java code may need to declare objects of class Plugin netscape. netscape .javascript javascript Accessing JavaScript Accessing JavaScript Package Package � 2 classes: JSObject and JSException � Create reference to JavaScript window JSObject window = JSObject.getWindow(this); � JSObject acts as a wrapper for JavaScript � Reference JavaScript objects and properties objects JSObject doc = (JSObject) � Commonly used JSObject methods include window.getMember(“document”); getWindow(), getMember(), setMember(), � Set object properties call() and eval() doc.setMember(“bgColor”, “blue”); � JSException used to throw exceptions when � Call JavaScript functions JavaScript errors occur window.eval(“alert(\”An alert message.\”);”);
JavaScript to Java Calling Java Methods JavaScript to Java Calling Java Methods 1) Directly Call Java Methods Java methods can be called directly in JavaScript � code: 2) Control Java Applets Var today = new java.util.Date(); 3) Control Java plug-ins System.out.println(today); Any public method or instance variable can be � accessed Greatly expands the library of Javascript code � available Controlling Java Applets Controlling Java Applets Applet methods and variables can be accessed Controlling Java Plug- -ins ins Controlling Java Plug � in Javascript code <APPLET CODE=“MyApplet.class” NAME=“MyApplet” Plug-ins written in Java can be controlled by � JavaScript WIDTH=100 HEIGHT=100> � To access the applet in JavaScript: <EMBED SRC=myAvi.avi NAME=MyEmbed document.MyApplet.<method name> WIDTH=100 HEIGHT=100> Applets can be started, stopped, restarted by In JavaScript, the document can be accessed by: � � JavaScript document.MyEmbed.<method or variable name> The applet can perform complex operations on � JavaScript’s behalf
Java to Plug- -ins ins Java to Plug Conclusion Conclusion � Plug-in must have a LiveConnect API that LiveConnect allows web developers to create � Java can use powerful, integrated web applications E.g. LiveAudio plug-in has public play() Opens up security concerns on the host � and stop() methods computer � Referencing plug-ins Compatibility issues with Internet Explorer and � other versions of Netscape Navigator Soundplayer plugin = (SoundPlayer) doc.getMember(“LiveAudioPluginName”); plugin.play();
Recommend
More recommend