WEB开发网
开发学院软件开发Java JSON ajax 页面输出工具 阅读

JSON ajax 页面输出工具

 2009-09-17 00:00:00 来源:WEB开发网   
核心提示:Java代码 importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.http.HttpServletResponse;importorg.json.JSONArray;importorg.json.JSONObject;/***A

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

Tags:JSON ajax 页面

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接