JavaScript: The Definitive Guide

23.1. Scripting Applets

In order to script an applet, you must first be able to refer to the HTML element that represents the applet. As discussed in Chapter 15, all Java applets embedded in a web page become part of the Document.applets[] array. Also, if given a name or id, an applet can be accessed directly as a property of the Document object. For example, the applet created with an <applet name="chart"> tag can be referred to as document.chart. And if you specify an id attribute for your applet, you can, of course, look up the applet with Document.getElementById().

Once you have the applet object, the public fields and methods defined by the applet are accessible to JavaScript as if they were the properties and methods of the HTML <applet> element itself. As an example, consider the Canvas applet defined in Example 22-14. If an instance of this applet is embedded in an HTML page with the id "canvas", the following code can be used to invoke methods of the applet:

var canvas = document.getElementById('canvas'); canvas.setColor(0x0000ff); canvas.fillRect(0,0,10,10); canvas.repaint( );

JavaScript can query and set the values of public fields of an applet, even fields that are arrays. Suppose that an applet with name="chart" defines two fields declared as follows (Java code):

public int numPoints; public double[] points;

A JavaScript program might use these fields with code like this:

for(var i = 0; i < document.chart.numPoints; i++) document.chart.points[i] = i*i;

This code snippet illustrates the tricky thing about connecting JavaScript and Java: type conversion. Java is a strongly typed language with a number of distinct primitive types; JavaScript is loosely typed and has only a single numeric type. In the previous example, a Java integer is converted to a JavaScript number, and various JavaScript numbers are converted to Java double values. There is a lot of work going on behind the scenes to ensure that these values are properly converted as needed. The topic of data conversion when JavaScript is used to script Java is covered in Chapter 12, and you may want to refer back to that chapter now. Part III also has useful entries for JavaObject, JavaArray, JavaClass, and JavaPackage. Note that Chapter 12 documents LiveConnect, a technology that originated with Netscape. Not all browsers use LiveConnect. IE, for example, uses its own ActiveX technology as the bridge between JavaScript and Java. Regardless of the technology underneath, the basic rules for converting values between Java and JavaScript are more or less the same in all browsers.

Finally, it is also important to note that Java methods can return Java objects, and JavaScript can read and write the public fields and invoke the public methods of those objects just as it can do with the applet object. JavaScript can also use Java objects as arguments to Java methods. Consider the Canvas applet of Example 22-14 again. It defines methods that return Shape objects. JavaScript code can invoke methods of these Shape objects, and it can pass them to other applet methods that expect Shape arguments.

Example 23-1 is a sample Java applet that does nothing but define a useful method for JavaScript to invoke. This getText() method reads a URL (which must come from the same server as the applet did) and returns its content as a Java string. Here's an example that uses this applet in a simple self-listing HTML file:

<!-- Self-listing HTML file using an applet to fetch a URL --> <body onload="alert(document.http.getText('GetText.html'));"> <applet name="http" code="GetTextApplet.class" width="1" height="1"></applet>

Example 23-1 uses basic Java networking, I/O, and text manipulation classes, but doesn't do anything particularly tricky. It simply defines a useful method and declares it public so that JavaScript can script it.

Example 23-1. An applet suitable for scripting

import java.applet.Applet; import java.net.URL; import java.io.*; public class GetTextApplet extends Applet { public String getText(String url) throws java.net.MalformedURLException, java.io.IOException { URL resource = new URL(this.getDocumentBase( ), url); InputStream is = resource.openStream( ); BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuilder text = new StringBuilder( ); String line; while((line = in.readLine( )) != null) { text.append(line); text.append("\n"); } in.close( ); return text.toString( ); } }

Категории