views:

288

answers:

4
public class Person {
  private String firstname;
  private String lastname;
  private PhoneNumber phone;
  private PhoneNumber fax;
  // ... constructors and methods
  private void calculate()
  {
  }
}

I have serialized the Java object located on the server side and sent it to the client

XStream xstream = new XStream(new DomDriver()); 

Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));

String xml = xstream.toXML(joe);

How can I deserialize that XML string into the Java object using JavaScript and execute the methods of person class in the client side using the JavaScript?

Please help me with syntax or any guidelines.

A: 

XML is presented as a DOM tree to JavaScript

Maurice Perry
ok, how does the javascript deserialize that recieved XML into Java object.since i want to deserialize into java object so that it could be executed in client side JVM once instantiating that java object
What you're describing here sounds different than your question.
cdmckay
why do you need JavaScript do deserialize the objects? why don't you simply use a Java-to-Java connection?
Maurice Perry
A: 

You can't run Java methods with Javascript. The only thing you could do is to read the properties of the Java object - this is the only information that is serialized in the XML file. It is very easy to read XML with javascript.

To be able to serialize a Java object, send it to a client and execute Java code there a totally different architecture would be needed. At first you need Java running on the client too. Then you would need to employ a method like RMI.

kgiannakakis
does <applet > satisfy meet my requirements sir?
Yes, but you still need a different serializing technique and dealing with security issues. The applet would probably need to be signed in order to be able to communicate with the RMI server.
kgiannakakis
This seems overly complicated. What he really needs is to make his Java server provide a web service and then he needs to call that web service using AJAX.
cdmckay
+1  A: 

You can call Java methods on the client side using JavaScript by using SOAP. This article explains how to create a WSDL web service that can be accessed by any SOAP client that supports WSDL.

You can then call the Java WSDL service using AJAX in JavaScript (if you can find a JS library that implements SOAP and WSDL).

Alternatively, you can write a simplified front-end to the Java WSDL service in PHP using PHP's built-in SoapClient library. Make it take some simple GET arguments and return JSON or XML. You could then trivially access the PHP web service using AJAX via jQuery (or an equivalent AJAX-supporting library).

cdmckay
+1 XStream supports JSON out of the box.
Fortyrunner
+1  A: 

If you're going for an applet and want to make Javascript calls from Java, checkout the LiveConnect with the JSObject wrapper class. This way you can excute javascript functions inside the applet (and pass information in between);

Executor exe = Executors.newSingleThreadExecutor();
final JSObject page = JSObject.getWindow(applet);

if (page == null) {
    /* Break here, no connection could be made */
}

final String javascriptFunction = "yourJavaScriptFunction()";
executor.execute(new Runnable() {
    public void run() {
        page.eval(javascriptFunction);
    }
});

Look into the IRIS applictation made for Flickr, it's open source and uses this technique. The Belgian JUG Parleys have a speech from a convention covering some of this, You can find it here.

Björn