简体   繁体   English

Java和JavaScript套接字通信

[英]Java and JavaScript socket communication

I am creating a browser using The Java DJ Project library. 我正在使用Java DJ Project库创建浏览器。 I need to create a persistence socket connection from the browser(Client) to a predefined server. 我需要创建一个从浏览器(客户端)到预定义服务器的持久套接字连接。 I want to put the Server IP address and Port at the browser side(Hard Coded) what will connect with that server. 我想将服务器IP地址和端口放在浏览器端(硬编码),它将与该服务器连接。 The socket connection could be created by Java or JavaScript. 套接字连接可以通过Java或JavaScript创建。 I want to do any option from the following 2 option. 我想从以下2个选项中进行任何选择。

  1. If I create the socket connection using java then there must be a way to send data using that connection from JavaScript. 如果我使用java创建套接字连接,则必须有一种方法可以使用JavaScript中的该连接发送数据。 I have an Applet that can do this thing. 我有一个Applet可以做到这一点。 You can check the solution at this link >> http://stephengware.com/proj/javasocketbridge/ But I don't want to use applet any more. 您可以在此链接>> http://stephengware.com/proj/javasocketbridge/中查看解决方案,但我不想再使用applet。 I want to call that Java socket connection from JavaScript or vice versa and send data without adding the applet into the page. 我想从JavaScript调用Java套接字连接,反之亦然,并发送数据而不将applet添加到页面中。 I just want to Call javascript function to send date which will use that Java socket. 我只想调用javascript函数来发送将使用该Java套接字的日期。

  2. Or I want to create that connection using JavaScript, but it should be persistence. 或者我想使用JavaScript创建该连接,但是应该是持久性的。 But if I do so then that connection should be used from Java to send the date. 但是,如果我这样做,则应该使用Java的连接来发送日期。

So will you please tell me the answer of some of my question... 所以,请您告诉我一些问题的答案...

  1. Which method should I apply between 01 or 02 (Described above)? 在01或02(如上所述)之间应采用哪种方法? And why? 又为什么呢?
  2. How to do that? 怎么做? Can anyone please provide me some code to do that? 谁能提供给我一些代码来做到这一点?

Would be very happy if anyone answer me. 如果有人回答我会很高兴。

Seeing as you tagged this JavaEE I would suggest using JavaScript in combination with Servlets(3.0) 看到您标记此JavaEE时,我建议将JavaScript与Servlets(3.0)结合使用

You would able to POST towards any of your servers servlet mapping which can read the request with its doPost() method. 您将能够向您的任何服务器Servlet映射POST,这些映射可以使用doPost()方法读取请求。

For the some code examples see this explanation by BalusC calling servlets using javascript 对于一些代码示例,请参见BalusC 使用javascript调用servlet的解释。

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

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