发送JSON数据的Servlet Applet的通信例子 [英] Sending JSON data Servlet Applet communication example
问题描述
喜正在开发中的servlet需要发送数据(数据库来)到小程序的应用程序我已经休耕本链接
Hi am developing an application in which servlet has to send data(coming from database) to applet i have fallowed this link
这是非常有用的,但他们提到传递数据通过JSON任何一个可以举这样一个例子。我试过对象的序列化,但我得到
java.io.EOFException
异常(我搜索解决方案,有人说这可能是JVM版本小姐比赛导致这一点),所以我决定去JSON。
it is very useful but they mentioned passing data through JSON can any one give an example of this. I tried serialization of object but am getting java.io.EOFException exception (i searched for solution some one says that may be JVM version miss match lead this) so i decided to go for JSON.
推荐答案
如果您要创建或读取JSON字符串,那么我建议你使用的谷歌的GSON 。
if you are going to create or read json strings, then I'd recommend using Google's Gson.
要发送从servlet的JSON响应做到这一点:
To send a json response from the servlet do this:
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = null;
try {
writer = response.getWriter();
writer.write(json);
} finally {
writer.close();
}
这篇关于发送JSON数据的Servlet Applet的通信例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!