Servelet 返回Json格式数据

/**
* [Product]
*     ExtJS4Study
* [Copyright]
*     Copyright © 2013 Changweihua All Rights Reserved.
* [FileName]
*     ListDept.java
* [History]
*     Version         Date            Author          Content
*     --------------- --------------- --------------- ---------------
*     1.0.0           2013-3-22       Changweihua     最初版本
*/
package net.cmono.servlet.lesson18;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.cmono.biz.DeptManager;
import net.cmono.model.Dept;
/**
* <b>Summary: </b>
*      TODO 请在此处简要描述此类所实现的功能。
* <b>Remarks: </b>
*      TODO 请在此处详细描述类的功能、调用方法、注意事项、以及与其它类的关系
*/
public class ListDept extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int parentId = 0;
if(request.getParameter("parentId") == null || request.getParameter("parentId").length() < 1){
parentId = 0;
}
else {
parentId = Integer.parseInt(request.getParameter("parentId"));
}
DeptManager deptManager  = null;
try {
deptManager = new DeptManager();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
List<Dept> depts = new ArrayList<Dept>();
try {
depts = deptManager.Select(parentId);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder sb = new StringBuilder("{totalCount:" + depts.size() + ",rows:[");
String tString = null;
if(depts.size()>0){
for (Dept dept : depts) {
sb.append("{"+dept.toString() + "},");
}
tString = sb.toString();
tString = tString.substring(0, tString.lastIndexOf(','));
}else {
tString = sb.toString();
}
tString += "]}";
System.out.println(tString);
PrintWriter out = response.getWriter();
out.write(tString);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

!

知识共享许可协议
《Servelet 返回Json格式数据》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

3 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     158.10ms