Can Java Applicaton and JavaScript running in the browser communicate with each other?

I have JavaScript embedded in a website running in a regular browser and I have a Java application running on the same computer. I am looking for a way to communicate and communicate with each other.

My guess is that creating files on the local filesystem from JavaScript running in a browser is out of the question.

The only way I have come up with is to use a server to which both programs can send messages, and from which they polled for new messages.

Is there any other way to do this?

+3


source to share


3 answers


Several ways I've seen in practice:

  • Java application can listen on some local port that your JS will access, for example via XHR. You will need to account for cross-site scripting (your JS may need to be loaded from that local url), but it's doable. The easiest way is to run the built-in HTTP server.
  • Your Java application can be registered as a protocoll handler in the OS. The JS will then open the links registered with the application, thereby sending data to it.


As @PavelHoral points out, CORS is a way to bypass the policy of the same origin .

+3


source


JavaScript inside the browser can only make AJAX requests, or bind to browser plugins if they provide some additional JS interface.

Local HTTP connection

One option is to listen for HTTP connections in your Java application (not necessarily a servlet).

You will need to handle CORS correctly .

Central server



Another option is to have a central server that both JS and Java code will connect to.

Java applet

Another option is to have a Java applet. When running in privileged mode, you can do pretty much anything (you can probably convert your Java application to a Java applet).

You will need to handle the applet's security here (for example, sign the applet with a trusted certificate).

+1


source


Please refer to this question for direct socket communication from javascript. You will need an HTML5 browser and it probably won't "just" work or necessarily be a good idea if you want to publish it to the WWW.

How do I use sockets in JavaScript \ HTML?

0


source







All Articles