Return JSON from servlet

前端 未结 4 1054
悲&欢浪女
悲&欢浪女 2020-12-03 01:58

It is a very basic request-response test. Browser sends \"hello from browser\" to servlet using jQuery $.ajax API, and servlet receives this message, then create a JSON obje

相关标签:
4条回答
  • 2020-12-03 02:26

    I was having the same problem. It was working well on Firefox but not on IE... I found out reading this post that my problem was related to the 'Content-Type'. The problem seems that that IE has problem with 'charset=UTF8'. However, if you use 'charset=UTF-8' (with a dash) it is then working! Your Content-Type should then be: application/json;charset=UTF-8

    0 讨论(0)
  • 2020-12-03 02:29
    <% 
         Gson gs = new Gson();
         BeanHelpBH bh = new BeanHelpBH();
         List<Baihatmoi> lst = bh.getTenbaihatbyName("Ao moi ca mau");
        String bha = gs.toJson(lst);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        out.print(bha);
        out.flush();
    
    
    %>
    
      script : 
            <script>
                 $(document).ready(function(){
                   $.get('jsontest.jsp',function(data){
                [enter image description here][1] console.log(data);
    
                  });
    
               });
            </script>
    
    0 讨论(0)
  • 2020-12-03 02:31

    using Gson you can send json response

    @WebServlet(urlPatterns = {"/jsonResponse"})
    public class JsonResponse extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        Student student = new Student(12, "Ram Kumar", "Male", "1234565678");
        Subject subject1 = new Subject(1, "Computer Fundamentals");
        Subject subject2 = new Subject(2, "Computer Graphics");
        Subject subject3 = new Subject(3, "Data Structures");
        Set subjects = new HashSet();
        subjects.add(subject1);
        subjects.add(subject2);
        subjects.add(subject3);
        student.setSubjects(subjects);
        Address address = new Address(1, "Street 23 NN West ", "Bhilai", "Chhattisgarh", "India");
        student.setAddress(address);
        Gson gson = new Gson();
        String jsonData = gson.toJson(student);
        PrintWriter out = response.getWriter();
        try {
            out.println(jsonData);
        } finally {
            out.close();
        }
    
    }
    

    }

    for more json response from servlet in java

    0 讨论(0)
  • 2020-12-03 02:34

    IE caches AJAX requests aggressively (more than Firefox, Chrome, and Safari, anyway). Sometimes you need to set cache header controller when request. Like cache:false. I tried to fix your code like this

    request.setCharacterEncoding("utf8");
            //response.setCharacterEncoding("utf8");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            JSONObject jsonObj = (JSONObject) JSONValue.parse(request.getParameter("para"));
            System.out.println(jsonObj.get("message"));
            JSONObject obj = new JSONObject();
            obj.put("message", "hello from server");
            out.print(obj.toString());
    

    I changed your response content-type from application/json; charset=utf8 to just application/json and that worked.

    0 讨论(0)
提交回复
热议问题