import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Ajax 页面输出工具
* @author panxiuyan
*
*/
public final class AjaxResponsePrintUtil {
/**
* 输出 JSONObject
* @param response
* @param obj
*/
public static void writer(final HttpServletResponse response,final JSONObject obj) {
initReponse(response);
PrintWriter pw = null;
try {
pw = response.getWriter();
pw.write(obj.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
/**
* 输出 JSONArray
* @param response
* @param obj
*/
public static void writer(final HttpServletResponse response, final JSONArray obj) {
initReponse(response);
PrintWriter pw = null;
try {
pw = response.getWriter();
pw.write(obj.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
private static void initReponse(HttpServletResponse response) {
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
}
}
JSON ajax 页面输出工具
最新推荐文章于 2025-12-29 14:42:26 发布
本文介绍了一个用于简化Ajax响应操作的工具类设计。该工具类支持输出JSON对象和数组,并初始化HTTP响应头以确保正确的数据格式和缓存控制。
1142

被折叠的 条评论
为什么被折叠?



