Ejemplo n.º 1
0
  protected void execute(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = response.getWriter();
    request.setCharacterEncoding("utf-8");
    String page = request.getParameter("page");
    EmpDAO dao = new EmpDAO();
    if (page != null) {
      int p = Integer.parseInt(page);
      List<Emp> empList = dao.findAll(p);
      JSONObject jobj = new JSONObject();
      jobj.put("ret", 0);
      jobj.put("msg", "ok");
      jobj.put("list", empList);
      out.print(jobj);
    } else {
      List<Emp> empList = dao.findAll();
      // 相应JSON的格式:{ret:xxxx,msg:xxxxx,......}
      // JSONArray arr = JSONArray.fromCollection(empList);
      JSONObject jobj = new JSONObject();
      jobj.put("ret", 0);
      jobj.put("msg", "ok");
      jobj.put("list", empList);
      out.print(jobj);
    }

    //        for (Emp emp : empList) {
    //            out.print(emp.toString()+"<br />");
    //        }
  }