简体   繁体   English

Web应用程序连接到外部服务器?

[英]Web appilcation connection to external server?

So I have two Java applications, a server and a client. 所以我有两个Java应用程序,一个服务器和一个客户端。 They are simple programs no GUI elements just console applications. 它们是简单的程序,没有GUI元素,只是控制台应用程序。 They work just fine running on my machine, and I have a client version that can even connect to the server via the internet. 它们在我的机器上运行得很好,并且我有一个客户端版本,甚至可以通过Internet连接到服务器。

毕加索

Instead of hosting the Server on my local machine I would like to host it from a site like Openshift. 与其在本地计算机上托管服务器,不如从Openshift这样的站点托管服务器。 Which I already have a HTML site up at using Tomcat 7. I would also like to be able to go to a page on the web server and have that page act as the client program. 在Tomcat 7上,我已经拥有一个HTML站点。我还希望能够转到Web服务器上的页面并将该页面用作客户端程序。

莫奈

I want to embedded the client program into the web server but.... 我想将客户端程序嵌入到Web服务器中,但是...

How do I make the connection between the two servers? 如何在两台服务器之间建立连接? Pretty much how do I get the Web Page to reach out to the other server and make the connection(I am using sockets)? 我几乎如何才能将Web页扩展到其他服务器并进行连接(我正在使用套接字)? Do I need to be using a servlet, JSP, or something like Jquery? 我是否需要使用servlet,JSP或类似Jquery的东西?

If you feel like you need to see either the server program or the client let me know and I will post them. 如果您感觉需要查看服务器程序或客户端,请告诉我,我将其发布。

I would like to host a client version on the web page. 我想在网页上托管客户端版本。

Your JSP or servlet would be the client in that case, it would open a socket to the server process. 在这种情况下,您的JSP或servlet将是客户端,它将打开服务器进程的套接字。 Have a look at HttpServlet and its doGet method, this is basically what you would implement, and where you would place much of your client code, like opening a socket to your server process and returning data. 看一下HttpServlet及其doGet方法,这基本上就是您要实现的内容,以及您将在其中放置许多客户端代码的地方,例如为服务器进程打开套接字并返回数据。 You'll find tons of examples on the internet, see for instance 您会在互联网上找到大量示例,例如

how to write hello world servlet Example 如何编写Hello World Servlet示例

Browsing to the servlet's URL will invoke the doGet method and execute your client code. 浏览到Servlet的URL将调用doGet方法并执行您的客户端代码。 It should be stateless though, ie take whatever parameters are in the HttpServletRequest, do its thing based on that, and return as soon as possible. 但是,它应该是无状态的,即获取HttpServletRequest中的任何参数,基于该参数执行其操作,并尽快返回。 If your clients need to retain their connection to the server process, it will be a different story, and you may need for instance websockets (I have insufficient experience with that but it would sound like a good fit in that case). 如果您的客户需要保持与服务器进程的连接,那就另当别论了,您可能需要例如websockets(我对此经验不足,但在这种情况下听起来很合适)。

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

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