zoukankan      html  css  js  c++  java
  • HttpServletResponse 工具类

    package com.gta.yyyf.commons.utils;
    
    import java.io.IOException;
    
    import javax.servlet.http.HttpServletResponse;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * HttpServletResponse帮助�?
     * 
     * @author
     * 
     */
    public final class ResponseUtils {
    	public static final Logger logger = LoggerFactory
    			.getLogger(ResponseUtils.class);
    
    	/**
    	 * 发送文本。使用UTF-8编码。
    	 * 
    	 * @param response
    	 *            HttpServletResponse
    	 * @param text
    	 *            发送的字符串
    	 */
    	public static void renderText(HttpServletResponse response, String text) {
    		render(response, "text/plain;charset=UTF-8", text);
    	}
    
    	/**
    	 * 发送json。使用UTF-8编码。
    	 * 
    	 * @param response
    	 *            HttpServletResponse
    	 * @param text
    	 *            发送的字符串
    	 */
    	public static void renderJson(HttpServletResponse response, String text) {
    		render(response, "application/json;charset=UTF-8", text);
    	}
    
    	/**
    	 * 发送xml。使用UTF-8编码。
    	 * 
    	 * @param response
    	 *            HttpServletResponse
    	 * @param text
    	 *            发送的字符串
    	 */
    	public static void renderXml(HttpServletResponse response, String text) {
    		render(response, "text/xml;charset=UTF-8", text);
    	}
    
    	/**
    	 * 发送内容。使用UTF-8编码。
    	 * 
    	 * @param response
    	 * @param contentType
    	 * @param text
    	 */
    	public static void render(HttpServletResponse response, String contentType,
    			String text) {
    		response.setContentType(contentType);
    		response.setHeader("Pragma", "No-cache");
    		response.setHeader("Cache-Control", "no-cache");
    		response.setDateHeader("Expires", 0);
    		try {
    			response.getWriter().write(text);
    			response.getWriter().flush();
    		} catch (IOException e) {
    			logger.error(e.getMessage(), e);
    		}
    	}
    }
    
  • 相关阅读:
    【转】关于char * 与 char[]
    网页打印js代码
    无法用排他锁锁定该数据库,以执行该操作。 (Microsoft SQL Server,错误: 5030)
    CKEditor使用笔记
    FormView作为单独编辑页笔记
    用WindowsMediaPlayer控件写个WinForm播放器
    ListView搭配DataPager控件实现分页笔记
    如何禁用ViewState
    C#获取本机IP搜集整理7种方法
    ListView高效率分页笔记
  • 原文地址:https://www.cnblogs.com/fuhengheng/p/8251491.html
Copyright © 2011-2022 走看看