Javascript Applet Communication
From LoveToKnow Web-Design
For new webmasters, it is sometimes difficult to understand javascript applet communication.
How Javascript Applet Communication Works
Javascript is a client-side programming script. This means that the processing of the script takes place on the user's browser, along with the rest of the HTML. The latest web browsers process the Javascript commands and functions. Likewise, Java applets are also client side applications, however they aren't processed entirely by the browser. Applets utilize the Java environment that users install as a browser plug-in. While this causes some complexity to the process of Javascript applet communication, the fact that the computer processes both programs as client-side scripts means that communication between the two is possible.
Netscape LiveConnect
LiveConnect is a feature provided by the Netscape browser to enable communication between Javascript and Java applets. With LiveConnect, you use Javascript to access variables, methods, classes and functions within the applet. The same applies in the other direction - through LiveConnect, Java applets can access Javascript functionality.
For this feature to work properly, you need to enable LiveConnect within Netscape Navigator. When LiveConnect is enabled, developers reference Java objects within Javascripts using syntax such as "java.lang.System" or "java.util.Date()".
The only limitation when using LiveConnect is that it only works on the Netscape browser. If you hope to reach a wide Internet population with your website, using LiveConnect may not be the best solution.
Javascript Applet Communication Techniques
There are a variety of techniques you can use to program communication between Javascript inside an HTML web page, and a Java applet running within the Java runtime environment.
Method #1 - Directly Call Java Applet Method
A common technique that works for most browsers is to define a public method in the Java applet, and then call that method from Javascript.
For example, the Java applet would have the following public method defined:
public void setString(String x)
{
txt.setString(x);
}
Within the HTML page, the following Javascript calls that public method within the applet named "jsap". The text in this example comes from a text field within a form on the web page:
function updateApplet()
{
document.jsap.setString(document.forms[0].txt1.value);
}
Method #2 - Using Applet Parameters
Another useful method to pass information from a web page to an applet using Javascript is if you pass values through the applet's "PARAM" tag. You can do this by defining the "VALUE" parameter of the Applet call as shown below:
<APPLET CODE="Demo.class"
NAME="app2" MAYSCRIPT
HEIGHT=200
WIDTH=200>
WIDTH=200>
<PARAM NAME="target"
VALUE="firstpage">
The class name must match the defined class as shown here:
public class Demo extends Applet implements ActionListener {The section of the Java class that reads the parameter value is shown here:
public void init() {
innertarget = getParameter("target");
Whenever the browser loads the applet code within the HTML page, it passes the parameter named "target" to the Java applet, which then assigns the value to the variable "innertarget" inside the Java applet.
Method #3 - Communication from the Applet to Javascript
While the above techniques provide methods to establish Javascript applet communication, it is also possible to communicate in the other direction. You can call Javascript functions from within a Java applet. You do this using Java's showDocument method as shown here:
public void init(){
String msg = "This is from the applet (using javascript alert)";
try {
getAppletContext().showDocument
(new URL("javascript:doAlert(msg)"));
}
}
The applet above, when called by the web page using the "APPLET" tag, will reach back to the Javascript code within that web page and call the "doAlert" function. You define that function within the scripting section of the HTML page as shown here:
<SCRIPT>
function doAlert(s) {
alert(s);
}
</SCRIPT>
The Power of Javascript Applet Communication
There are a number of important advantages to establishing communication between the Javascript within a web page, and an applet running on the client's computer. Java applets allow programmers to make use of a range of Java methods, functions and commands that are much more powerful than what Javascript alone is capable of. When you establish a method of passing information from Javascript to a Java applet, or you call Java applet methods from Javascript, you essentially have the ability to harness the power of Java from within your Javascript code. This can be extremely useful when you want to create a highly dynamic web page that offers users functionality that goes beyond the limitations of Javascript commands.
This page has been accessed 168 times. This page was last modified 04:42, 6 July 2008.
© 2006-2008 LoveToKnow Corp.
