applet 可以与 servlet 实例通信吗
我有一个使用 Http(不是套接字)与 servlet 进行通信的小程序。目前,applet 的每个实例(即,当每个 applet 由不同计算机上的不同客户端运行时),所有实例都与同一个 servlet 通信。我想要的是小程序的每个实例都与同一servlet的不同实例进行通信。这可能吗?
I have an applet that communicates with a servlet using Http (Not sockets). Currently, each instance of the applet (i.e. when each applet is run by a different client on a different computer), all the instances communicate with the same servlet. What I want is that each instance of the applet communicate with different instances of the same servlet. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不希望在 web 应用程序的生命周期中拥有同一 servlet 的不同实例。正常的做法是使用
HttpSession
来区分客户端。您需要传递HttpSession#getId()
作为相关小程序的参数:然后,在小程序中按如下方式连接 Servlet:
这里
servleturl
显然应该匹配web.xml
中 servlet 的url-pattern
。您也可以使用URLConnection.setRequestProperty()
设置Cookie
请求标头。最后,在 Servlet 中,要获取并存储客户端特定数据,请执行以下操作:
希望这会有所帮助。
You don't want to have different instances of the same servlet in webapp's lifetime. The normal practice is to use the
HttpSession
to distinguish between clients. You need to pass theHttpSession#getId()
as parameter to the applet in question:Then, in the Applet connect the Servlet as follows:
Here
servleturl
obviously should match servlet'surl-pattern
inweb.xml
. You can alternatively also set aCookie
request header usingURLConnection.setRequestProperty()
.Finally, in the Servlet, to get and store client specific data, do as follows:
Hope this helps.
从你的问题来看,你的 servlet 似乎包含状态。每个小程序都会有一个 会话 与您的 servlet 可以访问的 servlet 容器。您可以创建一个保存每个会话状态的对象,并将该对象放置为 调用者会话中的属性。这样,servlet 容器就可以在许多客户端之间自由共享一个 servlet 实例。
From your question it seems that your servlet contains state. Every applet will have a session with the servlet container which your servlet can access. You can create an object that holds the state per session and place that object as attribute in the session of the caller. This way the servlet container is free to share one servlet instance among many clients.
处理特定于实例的操作的常用方法是将信息存储在 servlet 容器提供的会话范围中,而不是将信息存储在 servlet 本身中。
为了使其正常工作,您的 applet 必须正确发送 Web 容器提供的 cookie 或 JSESSIONID 属性,或者 applet 必须请求 servlet 内的实例特定 URL。
我建议您进一步熟悉 Servlet API 规范,以便了解更多可用的内容。
另请注意,某些应用程序服务器支持“客户端”的概念,“客户端”是使用应用程序服务器提供的代码调用的程序,可以直接访问应用程序服务器代码的内部。实际的通信由应用程序服务器提供的库处理,因此这很简单。 Glassfish 和 Trifork 可以做到这一点。
The usual way to handle instance-specific actions is to have information stored in the session scope made available by the servlet container, not by having information stored in the servlet itself.
For it to work, your applet must correctly send cookies or the JSESSIONID attribute as provided by the web container or the applet must request a instance specific URL inside the servlet.
I would suggest you familiarize yourself further with the Servlet API specification in order to learn more about what is available to you.
Also note that some application servers support the notion of "clients" which are programs invoked with code served from the application server which have direct access to the inside of the application server code. The actual communication is handled by libraries also provided by the applcation server so this is simple. Glassfish and Trifork can do this.