Question

How I can send Java object from JavaScript to Java? LiveConnect allow us to get Java objects from calling applet method. i.e. we can have following method in applet:

public MyClass getMyClass() { return new MyClass(); }`

where MyClass is:

public class MyClass implements Serializable {
   private String a;
   private String b; //getters, setters
}

and in JS access this objects using:

applet.getMyClass().someMethod();

But how I can pass object from JavaScript (json object I think) to java method (not as json string)? i.e. I want to have in applet method like this:

public void myMethod(MyClass var)

and from JavaScript call this method passing parameter of type MyClass. How to build MyClass object in JS? I hope LiveConnect do conversion from JSON to Java object automatically..

Was it helpful?

Solution

I believe this should give you a good idea about how to go about this.

import netscape.javascript.*;

public class MyClass implements Serializable {
public String a;
public String b;

    public JavaDog(JSObject o) {
        this.a= (String)o.getMember("a");
        this.b = (String)o.getMember("b");
    }
}

Then you use new Packages.MyClass(yourJavaScriptObject) to create the object you want to pass to Java.

More information here:

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/LiveConnect_Overview

OTHER TIPS

Java LiveConnect documentation says :

When a Java object which was previously returned to the JavaScript engine is passed back to Java, the object is simply checked for assignment compatibility with the target type. The only exception to this rule is automatic conversion to java.lang.String.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top