Java代码
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");
}