发送各种数据到客户端的工具类

 

package cn.itcast.common.web;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

/**
 * 异步返回各种格式
 * json
 * xml
 * text
 * @author smn
 *
 */
public class ResponseUtils {

    //发送内容  
    public static void render(HttpServletResponse response,String contentType,String text){
        response.setContentType(contentType);
        try {
            response.getWriter().write(text);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //发送的是JSON
    public static void renderJson(HttpServletResponse response,String text){
        render(response, "application/json;charset=UTF-8", text);
    }
    //发送xml
    public static void renderXml(HttpServletResponse response,String text){
        render(response, "text/xml;charset=UTF-8", text);
    }
    //发送text
    public static void renderText(HttpServletResponse response,String text){
        render(response, "text/plain;charset=UTF-8", text);
    }
    
    
}

 

posted @ 2017-08-28 09:43  H.U.C-王子  阅读(174)  评论(0编辑  收藏  举报