小程序可以用一个servlet实例沟通 [英] Can an applet communicate with an instance of a servlet
问题描述
我有一个使用servlet的HTTP(不插槽)通信的小程序。目前,小应用程序(即,当每个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?
推荐答案
您不希望有相同的servlet 的不同的实例Web应用程序的生命周期。通常的做法是使用的HttpSession
来的客户进行区分。你需要通过 的HttpSession#的getId()
作为参数传递给有问题的小程序:
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 the HttpSession#getId()
as parameter to the applet in question:
<param name="jsessionid" value="${pageContext.session.id}">
然后,在小程序连接的Servlet如下:
Then, in the Applet connect the Servlet as follows:
String jsessionid = getParameter("jsessionid");
URL servlet = new URL(getCodeBase(), "servleturl;jsessionid=" + jsessionid);
URLConnection connection = servlet.openConnection();
// ...
的这里 servleturl
显然不应该在网络匹配servlet的
。您也可以还设置使用 URL模式
。 XML 饼干
请求头 URLConnection.setRequestProperty()
的
Here servleturl
obviously should match servlet's url-pattern
in web.xml
. You can alternatively also set a Cookie
request header using URLConnection.setRequestProperty()
.
最后,在Servlet中,获得和存储客户特定的数据,请执行以下操作:
Finally, in the Servlet, to get and store client specific data, do as follows:
// Store:
request.getSession().setAttribute("data", data);
// Get:
Data data = (Data) request.getSession().getAttribute("data");
希望这有助于。
这篇关于小程序可以用一个servlet实例沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!