JavaMoving data to the browser using Java and JavaScript Part 2: On...

Moving data to the browser using Java and JavaScript Part 2: On the browser

June 29, 1999
Moving data to the browser using Java and JavaScript
Part 2: On the browser

by Jason Bloomberg

In Part 1, I showed you a simple way to move data from back-end databases to Web servers using Server-Side LiveConnect, which is Netscape’s name for its Java to JavaScript protocol. In this article, I will cover Client-Side LiveConnect, which allows the Java in an applet and the JavaScript on a Web page to communicate. And, yes, it works in both Communicator and Internet Explorer.

Prerequisites

If you’ve never before worked with LiveConnect, I recommend that you read the following three articles:

These articles cover all the basics of LiveConnect, and also provide several useful examples. My last article touched upon how to access Java objects in JavaScript using a JavaScript wrapper for the Java object (see Listing 3) This article concentrates on a different aspect of LiveConnect: accessing JavaScript objects in Java by using the

netscape.javascript.JSObject
object.

Why access JavaScript objects from Java?

In a browser environment, JavaScript code and Java applets can accomplish very different tasks. Because JavaScript affects the overall operation of the browser, it has access to frames, forms, form elements, and more. Java applets, however, operate in a “sandbox,” which prevents them from controlling the browser directly. On the other hand, Java supports socket connections, streams, and threading, which allow a page to maintain a continually open connection with the server. As long as the user’s firewall permits, an applet can send and receive information from the server without reloading the page. This is why, for example, most of today’s chat clients are written in Java. (It is possible to write a chat client in JavaScript, but that client must poll the server: it must reload a page every few seconds in order to send and receive the chat messages).

Therefore, just as LiveConnect can be used to move a database Result Set to a JavaScript Array, LiveConnect can also take a JavaScript Array of any type and move it into a Java object that can then be uploaded to the server. You can use this capability to build a JavaScript-based chat client, multi-player game, or any other page-based script that involves direct interaction with the server.

Getting started

Netscape has provided a package with Communicator, called

netscape.javascript
, that contains the JSObject class, as well as some additional classes for supporting LiveConnect. To write an applet that will access JavaScript objects in Communicator, you must import the

netscape.javascript
package:


import netscape.javascript.*;
To make your applet compatible with Internet Explorer, you must include the appropriate jar file from Netscape with your own class files. You can get this file when you download Communicator; it has different names depending on the version of the browser you are using. For example, I found the

netscape.javascript
package in a file called java40.jar, which was located in the directory “Netscape Communicator Folder/Essential Files/Java/Lib.” Simply upload this file into the same directory as your own class files.

Secondly, you must allow your applet to access JavaScript by placing the

MAYSCRIPT
attribute in your

APPLET
tag. You don’t need this attribute if you only want your JavaScript to access your Java; you only need it if you want your Java to access the JavaScript.

Accessing JavaScript objects

The first step to accessing JavaScript from Java is to define a handle for your browser window. Because the window is the top of the JavaScript object hierarchy, once your applet can access the window, it can access the entire object hierarchy.

You may want to create the window handle in your applet’s

init ()
method, so it will be available throughout the operation of the applet:

import netscape.javascript.*;

public class thisApplet extends Applet
{
private JSObject window;

public void init ()
{
window = JSObject.getWindow (this);
// where “this” indicates the applet itself

}

}

Now that you have a reference to the browser window, you can access its objects. There are two basic ways of accessing JavaScript objects: using the

JSObject.eval ()
method, or using the

JSObject.getMember ()
method.

Example 1: Accessing Form Elements from Java

Say you have a simple HTML form, defined as follows:


You can refer to the field

myForm.myField
from Java by using the

JSObject.eval ()
method:

JSObject myField =
(JSObject) window.eval (“document.myForm.myField”);

You can then access the text field’s value by using the

JSObject.getMember ()
method:

String myFieldValue =
myField.getMember (“value”).toString ();

(Ed. note: Some code lines broken to fit format.)

Example 2: Calling a JavaScript function from Java

You can call a JavaScript function by using the

JSObject.call ()
method. This method takes a String and an Array as arguments; the String is the name of the function, and the Array contains the arguments to the function. For example, to call a JavaScript alert from Java, you can write:

window.call (“alert”, {“This alert came from Java!”});

Note that the second argument is actually an Array with one element, namely the String you want to send to the alert dialog.

Example 3: Accessing a JavaScript Array from Java

Let’s say you have defined a JavaScript Array:

myArray = new Array (new Array (“A”, “B”, “C”),
new Array (“D”, “E”, “F”), new Array (“G”, “H”, “I”));

You can use the

JSObject.eval ()
method to convert this JavaScript Array into a JSObject:

JSObject myArray = (JSObject) window.eval (“myArray”);

Now, however, we have a problem. We cannot simply cast

myArray
to an object of type Array! We can use

myArray
to call the JavaScript methods of the JavaScript Array type, like

concat ()
,

join ()
, or

splice ()
, but none of these methods allows us to access the elements of the Array. Therefore, this approach isn’t very useful. We must take a different approach.

The approach we must take is to create our own JavaScript wrapper for the JavaScript Array object, so that Java can access the Array’s elements:

function getElement (i, j) // to be used in the wrapper
{
return this.myArray [i][j];
}

function arrayWrapper (thisArray)
{
this.myArray = new Array ();
this.myArray = thisArray;
this.getElement = getElement;
// getElement is now a method of arrayWrapper
return this;
}

myArrayWrapper = new arrayWrapper (myArray);

Now, instead of having Java access the

myArray
Array directly, it should only access the

myArrayWrapper
object:

JSObject myArray =
(JSObject) window.eval (“myArrayWrapper”);

If we want to access, say, the value of

myArray [1][2]
, we would write:

String thisValue =
myArray.call (“getMember”, {1, 2}).toString ();
// will yield “F”, because myArray [1][2] == “F”.

And that is how we access the elements of a JavaScript Array from Java.

Next steps

In the above example, I showed you how to access elements in a JavaScript Array that happen to be Strings. However, JavaScript Arrays can hold all manner of different kinds of objects. To access a general JavaScript Array in Java, you would need to identify which type of data each element contains and deal with it separately.

Conclusion

LiveConnect is an extremely powerful, yet underutilized capability of today’s browsers. It allows Java applets to take full advantage of client-side JavaScript, and vice-versa. In particular, because Java can be used to maintain a connection with the server, and hence any back-end data source, LiveConnect can be an important way to build Web pages with direct access to back-end databases.


About the author

Jason Bloomberg has been coding, scripting, and programming Web sites since early 1995. He is now director of Web technology at TransNexus LLC, an Internet telephony company, but is best known for his JavaScript games at The Rhodes Arcade. His book, Web Page Scripting Techniques, was published by Hayden Books in 1996. He has two children and lives in Atlanta, Ga.


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories