小程序可以用一个servlet实例沟通 [英] Can an applet communicate with an instance of a servlet

查看:127
本文介绍了小程序可以用一个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屋!

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