发送 JSON 数据 Servlet Applet 通信示例 [英] Sending JSON data Servlet Applet communication example

查看:26
本文介绍了发送 JSON 数据 Servlet Applet 通信示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中 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.EOF异常异常(我搜索了解决方案,有人说这可能是 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 字符串,那么我建议使用 Google 的 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屋!

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