与Tomcat服务器上servlet的Java小程序进行通信 [英] Java applet communicate with servlet on tomcat server

查看:1173
本文介绍了与Tomcat服务器上servlet的Java小程序进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出如何处理这个问题,但我不知道从哪里开始。

我用Java编写的小程序的游戏,我试图使跨网络的多玩家。目前,它是一个独立的应用程序,但最终它需要在一些网页,在这里客户可以通过浏览器访问,并互相对战。

我在想,每个客户端小程序可以使用套接字与持续运行在Tomcat服务器上的applet通信。在服务器上的这个小程序将处理传入套接字连接,创建新的线程来处理每个连接,保持连接的客户端列表,并设置游戏。客户还可以通过与服务器通信获得有关过去的游戏等数据库的信息。

例如我想有一个客户端,以便能够开始游戏。然后,其他客户端通知他已经开始了游戏,并且可以加入游戏。然后,当主机点击开始游戏,所有其他客户端通知,并在游戏中的小程序开始。那么谁首先解决了这个难题会点击完成。他的得分将被发送到服务器小程序然后转发结果向连接每个客户端

所以我有一些问题


  1. 这听起来像一个合理的计划?


  2. 如何tomcat的协助执行?


  3. 我将如何将其部署到Tomcat?我在eclipse这样做,并且已经设置添加本地Tomcat服务器。我只是运行在Eclipse服务器上的类,那么套接字连接到的http://本地主机/包名/类名小程序?


  4. 一下班就在servlet需要扩展,使其连接?由于连接将从一个applet做出应该在servlet是一个非HTTP Servlet的?


  5. 您能推荐一些文档或提供客户端小程序使用Tomcat服务器小程序通信的一些示例code?我见过的所有例子是使用HTTP信息传递给一个servlet只是HTML表单。



解决方案

如果您的应用程序不需要实时通信超过原始套接字服务器(这听起来像你没有),你可能会更好使用标准Web协议为您的小程序与服务器进行通信。

一种选择可能是HTTP / JSON。您的小程序可以让HTTP连接到一个servlet,而servlet将生成一个JSON对象,重新presents服务器需要发送到客户端的消息。客户端将启动一个线程,将循环,进行异步阻塞调用你的servlet来轮询新的数据。

这种方法的主要优点是,在未来,你应该会喜欢,那么能够摆脱客户端的Java小程序,并与HTML5和Javascript取代它。

GSON 是用于JSON序列化/反序列化的图书馆。和 的java.net.URL 是你可以使用连接到servlet的小程序里面是什么。

I've been trying to work out how to approach this problem but I don't know where to start.

I have an game applet written in Java that I'm trying to make multi-player across a network. At the moment it is a standalone application but eventually it needs to be on some sort of web page where clients can access it through a browser and play against each other.

I was thinking that each clients applet could communicate using sockets with an applet that runs constantly on a tomcat server. This applet on the server would handle incoming socket connections, create new threads to handle each connection, maintain a list of connected clients, and set up games. Clients could also get information from a database about past games etc. by communicating with the server.

For example I want a client to be able to start a game. Then other clients are notified that he has started a game and can join the game. Then when the host clicks start game, all the other clients are notified and the game begins in their applet. Whoever solves the puzzle first would then click finished. His score would be sent to the server applet which then forwards the result to each client connected.

So i have some questions

  1. Does this sound like a reasonable plan?

  2. How does tomcat assist with this?

  3. How would i deploy it to tomcat? I'm doing this in eclipse and have set added a local tomcat server. Do i just run the class on the server with eclipse, then socket connect to http://localhost/packagename/classname on the applet?

  4. What classes would the servlet need to extend to make it connectable? Since connections will be made from an applet should the servlet be a Non-Http Servlet?

  5. Can you recommend some documentation or provide some example code of a client applet communicating with a server applet using tomcat? All the examples I've seen are just html forms that pass information to a servlet using Http.

解决方案

If your application doesn't need real-time communication with a server over a raw socket (and it sounds like yours does not), you might be better off using standard web protocols for your applet to communicate with the server.

One choice might be HTTP/JSON. Your applet could make an HTTP connection to a servlet, and that servlet will generate a JSON object that represents a message the server wants to send to the client. The client will start a thread that will loop, making an asynchronous blocking call to your servlet to poll for new data.

The major advantage of this approach, is that in the future, should you be so inclined, you can ditch the client-side java applet, and replace it with HTML5 and Javascript.

GSON is a library to use for JSON serialization/deserialization. And java.net.URL is what you would use to connect to the servlet inside your applet.

这篇关于与Tomcat服务器上servlet的Java小程序进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆