Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Some day your boss may ask you to integrate a Java applet with some JavaScript code. How would you accomplish that task? For starters, you should know the basics of communication between JavaScript and Java. This article examines those basics. It first investigates how JavaScript code talks to a Java applet and then looks at the reverse scenario: the applet talking to JavaScript code.
Note: I created and tested this article's code using version 1.4.1_05 of Sun's Java 2 SDK, the Java plug-in for that version's associated Java Runtime Environment, the Internet Explorer 5.50.4134.0100 Web browser, and the Netscape Navigator 6.2.3 Web browser. The underlying platform for all that software was Microsoft Windows ME.
Modern Web browsers use the document object model (DOM) to expose a Webpage's elements as programmable components, with their
own sets of properties and methods. DOM exposes the current Webpage via the document object and—via the id or name attributes—treats named applet, form, and other HTML elements as document's properties. For example, suppose that you specify the following applet element in an HTML file:
<applet code="Calculator.class" name="Calc" width=200 height=200>
</applet>
According to DOM, Calc is a property of Document, and document.Calc refers to that property. If the applet exposes any public fields or methods, JavaScript can access them through DOM. For
example, assume Calculator exposes method public void clear() and field public String value. In JavaScript, access both members via document.Calc.clear (); and document.Calc.value.
Let's put this knowledge to good use. Listing 1's JSTOJ.html text specifies applet JSTOJ, form JSTOJForm, and a pair of JavaScript functions. The callJava() function updates the applet's displayed message and background color when either the form's text-field content changes (and
focus leaves the field) or a selection is made from the drop-down list of color names:
Listing 1. JSTOJ.html
<html>
<head>
<title>JavaScript To Java</title>
<script language="JavaScript1.3">
var JSTOJForm;
function init ()
{
JSTOJForm = document.JSTOJForm;
}
function callJava ()
{
document.JSTOJ.msg = JSTOJForm.msg.value;
document.JSTOJ.setColor (JSTOJForm.colors.options
[JSTOJForm.colors.selectedIndex].value);
}
</script>
</head>
<body onload="init ()">
<applet code="JSTOJ.class" name="JSTOJ" width=300 height=300>
</applet>
<p>
<form name="JSTOJForm">
<input name="msg" type="text" size="30" onchange="callJava ()">
<select name="colors" size="1" onchange="callJava ()">
<option value="black" selected>black</option>
<option value="red">red</option>
<option value="magenta">magenta</option>
<option value="blue">blue</option>
</select>
</form>
</body>
</html>
When either of the onchange or onload event handlers invokes callJava(), JSTOJForm.msg.value returns the text content of the form's msg text field, which is assigned to the document.JSTOJ applet's public msg field. Similarly, JSTOJForm.colors.options [JSTOJForm.colors.selectedIndex].value returns the selected text content of the form's colors drop-down list field, which passes as an argument to the applet's public setColor() method. Listing 2 presents that applet's source code:
Archived Discussions (Read only)