weixin_33693070 2016-07-14 14:43 采纳率: 0%
浏览 271

从Servlet发送JSON

I write this servlet :

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("application/json");

    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
    String json = "";
    if(br != null){
        json = br.readLine();
    }

    PrintWriter out = response.getWriter();

    out.write(json);
    out.flush();
}

Sending data via ajax request and the servlet me return them. The problem is that when I try to make me return a JSONObject created inside servlet gives me error 500.

The code that gives me this error:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {




    response.setContentType("application/json");

    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));

    JSONObject json = new JSONObject();

    json.put("name", "federico");

    PrintWriter out = response.getWriter();

    out.print(json);
    out.flush();
}

My requestAjax is :

$.ajax({
                url: "ValidServlet",
                type: 'POST',
                data : JSON.stringify(submitFormData),
                success: function (data) {
                    alert(data.name);
                },
                error: function (data) {
                    alert('error');
                }
            });

My error is :

Failed to load resource: the server responded with a status of 500 (Internal Server Error) (17:33:34:492 | error, network)at http://localhost:8080/ProgettoProsa/ValidServlet> 

Maybe I'm wrong to return so the json?

Help me figure out where I'm fail.

Thank you all.

  • 写回答

1条回答 默认 最新

  • weixin_33720078 2016-07-14 18:15
    关注

    try this code to send json data with http:

        package com.nl.services.ws.sincronizacao;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.google.gson.Gson;
    
    @WebServlet(value = "/TestGsonHttp")
    public class TestGsonHttp extends HttpServlet {
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            // Your class
            MyJson m = new MyJson();
            m.setName("frederico");
    
            Gson g = new Gson();
            String jsonResult = g.toJson(m);
    
            PrintWriter writer = resp.getWriter();
            writer.print(jsonResult);
            writer.flush();
            writer.close();
        }
    
        class MyJson {
    
            private String  name;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
        }
    }
    

    评论

报告相同问题?

悬赏问题

  • ¥15 三菱变频器模块怎么检测触发
  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导