如何使用JavaScript处理Java对象序列化?

时间:2021-09-15 16:55:40
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

我已经将位于服务器端的Java对象序列化并将其发送到客户端

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?

如何使用JavaScript将XML字符串反序列化为Java对象,并使用JavaScript在客户端执行person类的方法?

Please help me with syntax or any guidelines.

请帮我解释语法或任何指南。

4 个解决方案

#1


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.

您可以使用SOAP在客户端调用Java方法。本文介绍如何创建可由任何支持WSDL的SOAP客户端访问的WSDL Web服务。

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

然后,您可以使用JavaScript中的AJAX调用Java WSDL服务(如果您可以找到实现SOAP和WSDL的JS库)。

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).

或者,您可以使用PHP的内置SoapClient库在PHP中编写简化的Java WSDL服务前端。使它采用一些简单的GET参数并返回JSON或XML。然后,您可以通过jQuery(或等效的支持AJAX的库)使用AJAX轻松访问PHP Web服务。

#2


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);

如果您要使用applet并希望从Java进行Javascript调用,请使用JSObject包装类检查LiveConnect。这样你就可以在applet中执行javascript函数(并在其间传递信息);

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.

查看为Flickr制作的IRIS应用程序,它是开源的并使用这种技术。比利时JUG Parleys在会议上发表演讲,其中包括其中一些,你可以在这里找到它。

#3


XML is presented as a DOM tree to JavaScript

XML作为JavaScript的DOM树呈现

#4


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.

您无法使用Javascript运行Java方法。您唯一能做的就是读取Java对象的属性 - 这是在XML文件中序列化的唯一信息。使用javascript读取XML非常容易。

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.

为了能够序列化Java对象,将其发送到客户端并在那里执行Java代码,将需要完全不同的架构。首先,您还需要在客户端上运行Java。然后你需要使用像RMI这样的方法。

#1


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.

您可以使用SOAP在客户端调用Java方法。本文介绍如何创建可由任何支持WSDL的SOAP客户端访问的WSDL Web服务。

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

然后,您可以使用JavaScript中的AJAX调用Java WSDL服务(如果您可以找到实现SOAP和WSDL的JS库)。

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).

或者,您可以使用PHP的内置SoapClient库在PHP中编写简化的Java WSDL服务前端。使它采用一些简单的GET参数并返回JSON或XML。然后,您可以通过jQuery(或等效的支持AJAX的库)使用AJAX轻松访问PHP Web服务。

#2


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);

如果您要使用applet并希望从Java进行Javascript调用,请使用JSObject包装类检查LiveConnect。这样你就可以在applet中执行javascript函数(并在其间传递信息);

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.

查看为Flickr制作的IRIS应用程序,它是开源的并使用这种技术。比利时JUG Parleys在会议上发表演讲,其中包括其中一些,你可以在这里找到它。

#3


XML is presented as a DOM tree to JavaScript

XML作为JavaScript的DOM树呈现

#4


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.

您无法使用Javascript运行Java方法。您唯一能做的就是读取Java对象的属性 - 这是在XML文件中序列化的唯一信息。使用javascript读取XML非常容易。

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.

为了能够序列化Java对象,将其发送到客户端并在那里执行Java代码,将需要完全不同的架构。首先,您还需要在客户端上运行Java。然后你需要使用像RMI这样的方法。