如何发送从servlet的对象列表小程序吗? [英] How to send list of objects from servlet to applet?

查看:193
本文介绍了如何发送从servlet的对象列表小程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回的对象列表小程序。
如何实现呢?

I want to return list of objects to applet. How to implement it?

推荐答案

将其转换为字符串在一个共同的传输格式。例如, JSON ,的 XML CSV 。这些常见的传输格式的优点在于,有这么多的API /库提供给复杂的Java对象,并在公共传输格式用最少需要code的字符串或输入/输出流之间进行转换。最后,你只是把它写到随后将作为applet的URL连接的输入流被检索的HTTP servlet响应的输出流。

Convert it to a string in a common transfer format. For example, JSON, XML or CSV. Those common transfer formats have the advantage that there are so many APIs/libraries available to convert between a complex Java object and a String or input/output stream in the common transfer format with a minimum of needed code. Finally you just write it to the output stream of the HTTP servlet response which will then be retrieved as input stream of the URL connection in the applet.

例如, GSON 的可用于一个复杂的转换之间Java对象和JSON格式的字符串如下。在servlet它会是这个样子:

For example, Gson can be used to convert between a complex Java object and a string in JSON format as follows. In the servlet it would look something like this:

Data data = createAndPopulateItWithLists();
String json = new Gson().toJson(data);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

在该applet它应该是这样的:

In the applet it would look like this:

InputStream input = new URL(getCodeBase(), "servletPath").openStream();
Data data = new Gson().fromJson(new InputStreamReader(input, "UTF-8"), Data.class);
// ...

这篇关于如何发送从servlet的对象列表小程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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