JSON ajax 页面输出工具


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


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3