Top
Interstage Studio J Business Kit User's Guide
FUJITSU Software

2.3.15 JavaScript Communication

This section describes the JavaScript communication of JBK Plugin.

JBK Plugin supports the JavaScript communication with Internet Explorer.

This section describes the following items:

2.3.15.1 Calling JavaScript from an applet

To call JavaScript from Java applet, it is necessary that Java communicates with JavaScript. JBK Plugin enables Java to communication with JavaScript using netscape.javascript.JSObject object, which is instance of Java wrapper class, in Internet Explorer.

The class file for the JSObject class and JSException class is stored in the jar file for the development of the JBK plugin ("classes\jbkstd.jar" in the JBK installation folder). When compiling the class of an applet that uses the PluginAppletContext interface, check whether the jar file for the development of the JBK plugin is included in the classpath.

Note

Observe the following notes derived from Internet Explorer limitations when using JavaScript Communication in Internet Explorer:

  • The user cannot use setMember method to set the properties of objects excluding window and document objects.

  • The user cannot use eval emthod excluding window objects.

  • Because of the security restrictions of Windows XP Service Pack 2, you must use absolute path instead of relative path to designate local files under the following conditions.

    • When opening a local file by using open method of the window object.

    • When setting a local file by using location property of the window object.

  • You cannot use getMember method to get the properties of some objects.

  • The type of the return value of the following methods of the Date object is Double in Internet Explorer in Netscape though it is Integer.

    • getYear

    • getMonth

    • getDate

    • getHours

    • getMinutes

    • getSeconds

    • getDay

    • getTimezoneOffset

Information

JSObject also supports the array of JavaScript. To get an array from applet using JavaScript, please refer to the following code.

import netscape.javascript.*;
import java.applet.*;

public class Applet1 extends Applet {

     public void init() {
            JSObject win = JSObject.getWindow(this);

            /* Array is set as 'arrayname = new Array("0", "1", "2", ...)' */

            // get JavaScript Array object
            JSObject array = (JSObject)win.getMember("arrayname");
            // get a number of index
            (Integer)array.getMember("length");
            // get the first member of the array
            (String)array.getMember("0");
     }
}

2.3.15.2 Calling a method of an applet from JavaScript

JBK Plugin allows calling a public method of an applet from JavaScript.

If the calling public method is overloaded, it cannot be guaranteed which of the methods is called.

2.3.15.3 Accessing cookie information

This section describes how to access cookie information.

An applet can access cookie by using cookie property of JavaScript document object which is referred via JSObject.

Example

Gets the cookie information from the browser, and sends it to the server.

import java.applet.*;
import java.net.*;
import netscape.javascript.*;

public class Applet1 extends Applet {
    public void init(){
        JSObject win = JSObject.getWindow(this);

        // refer the cookie property of JavaScript document object
        JSObject doc = (JSObject)win.getMember("document");
        String cookie = (String)doc.getMember("cookie");

        try{
            /* set the URL to send the cookie */
            // get codebase of the applet
            URL url = getCodeBase();
            String strurl = url.toString();

            // set URL
            /* in this case, "/servlet/cookie" */
            strurl = strurl + "/servlet/cookie";
            url = new URL(strurl);

             // set the HTTP request property
             HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
             urlCon.setRequestProperty("cookie", cookie);

             urlCon.disconnect();

             /* Describe the procedure using HttpURLConnection */

        }catch(Exception e){
             e.printStackTrace();
        }
    }
}