简体   繁体   English

Java Applicaton和在浏览器中运行的JavaScript可以相互通信吗?

[英]Can a Java Applicaton and a JavaScript running in a browser communicate with each other?

I have a JavaScript embedded in a website, running in a normal browser, and I have a Java Application running on the same machine. 我在网站中嵌入了JavaScript,在普通浏览器中运行,并且我在同一台机器上运行了Java应用程序。 I am looking for a way have both communicate with each other. 我正在寻找一种既能相互沟通的方式。

I assume that creating files in the local file system from inside JavaScript running in a browser is out of question. 我假设从浏览器中运行的JavaScript内部创建本地文件系统中的文件是不可能的。

The only way I came up with would be to use a server that both programs can send messages to, and from which they poll for new messages. 我提出的唯一方法是使用一个服务器,两个程序都可以发送消息,并从中轮询新消息。

Is there any other other way to accomplish this? 还有其他方法可以做到这一点吗?

A couple of ways I saw in practice: 我在实践中看到的几种方式:

  • You Java Application may listen to some local port which your JS will access for instance via XHR. Java应用程序可能会侦听您的JS将通过XHR访问的一些本地端口。 You'll need to mind cross-site scripting (your JS may need to be loaded from that local URL), but this is doable. 您需要注意跨站点脚本(您的JS可能需要从该本地URL加载),但这是可行的。 The easiest would probably be to run an embedded HTTP server. 最简单的可能是运行嵌入式HTTP服务器。
  • Your Java Application may be registered as a protocoll handler in the OS. 您的Java应用程序可能在操作系统中注册为协议处理程序。 Then JS would open links registered with the application thus sending data to it. 然后,JS将打开向应用程序注册的链接,从而向其发送数据。

As @PavelHoral is pointing out, CORS is a way to workaround same-origin policy . 正如@PavelHoral指出的那样, CORS是一种解决同源策略的方法

JavaScript inside a browser can only make AJAX requests or communicate with browser plugins if they provide some additional JS interface. 浏览器中的JavaScript只能提供AJAX请求或与浏览器插件通信,如果它们提供一些额外的JS接口。

Local HTTP connection 本地HTTP连接

One option is to listen for HTTP connections in your Java application (does not have to be servlet). 一种选择是在Java应用程序中侦听HTTP连接(不必是servlet)。

You will need to handle CORS correctly . 您需要正确处理CORS

Central server 中央服务器

Other option is to have a central server to which both your JS and Java code will connect. 其他选择是拥有一个中央服务器,JS和Java代码都将连接到该服务器。

Java applet Java小程序

Another option is to have Java applet. 另一个选择是拥有Java applet。 When running in privileged mode you can do pretty much anything (maybe you can convert your Java application to Java applet). 在特权模式下运行时,您可以执行任何操作(也许您可以将Java应用程序转换为Java applet)。

You will need to handle applet security here (eg signing applet with trusted certificate). 您需要在此处理applet安全性(例如,使用可信证书签署applet)。

Please refer to this question for direct sockets communications from within javascript. 请参阅此问题,以便在javascript中直接进行套接字通信。 You'll need a HTML5 browser, and likely this won't "just" work or necessarily be a good idea if you want to publish this on the WWW. 你需要一个HTML5浏览器,如果你想在WWW上发布它,这可能不会“只是”工作或者必然是个好主意。

How to Use Sockets in JavaScript\\HTML? 如何在JavaScript \\ HTML中使用套接字?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM