如何通过extjs从servlet获取数据

时间:2013-10-26 10:37:36

标签: asp.net extjs

我创建一个jsonarray并从servlet发送,然后发送到前端, 正面应该获取消息和currentTime,并在网站上显示它们。我该如何在EXTjs中做到这一点:
以下是servelet中的代码:

public void loadHistory(HttpServletRequest request, HttpServletResponse response) throws IOException{

        JsonObject json = new JsonObject();
        JsonArray dataArray = new JsonArray();
        String groupName = request.getParameter("groupName");
        String chatRoomName = getChatRoom(groupName);

        Database db = new Database(chatRoomName);
        CouchDbClient dbClient = db.getDbClient();

        PrintWriter out = response.getWriter();


        int i=0;
        while (dbClient.contains(String.valueOf(i++))){
            JsonObject objHistory = dbClient.find(JsonObject.class, String.valueOf(i++));
            String preMessage = objHistory.get("message").getAsString();
            String preTime = objHistory.get("currentTime").getAsString();
            json.addProperty("message", preMessage);
            json.addProperty("currentTime", preTime);
            dataArray.add(json);
        }
        if (dataArray!=null){
            response.setContentType("application/json");
            out.print(dataArray);
            out.close();
        }
    }

1 个答案:

答案 0 :(得分:0)

在发送响应之前修改if条件中的以下代码。

  if (dataArray!=null){
         response.setContentType("application/json");
         JSONObject resultJsonMain=new JSONObject();
         resultJsonMain.put("resultJsonMain",dataArray);
         out.println(resultJsonMain);

        out.close();
    }