Http与Servlet工具类

1、Content Type 定义

public static final String TEXT_TYPE = "text/plain";
    public static final String JSON_TYPE = "application/json";
    public static final String XML_TYPE = "text/xml";
    public static final String HTML_TYPE = "text/html";
    public static final String JS_TYPE = "text/javascript";
    public static final String EXCEL_TYPE = "application/vnd.ms-excel";

2、Header 定义

public static final String AUTHENTICATION_HEADER = "Authorization";

3、常用数值定义

 public static final long ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;

    public static final String HEADER_ENCODING = "encoding";
    public static final String HEADER_NOCACHE = "no-cache";
    public static final String DEFAULT_ENCODING = "UTF-8";
    public static final boolean DEFAULT_NOCACHE = true;

4、设置禁止客户端缓存的Header

public static void setDisableCacheHeader(HttpServletResponse response) {
        // Http 1.0 header
        response.setDateHeader("Expires", 1L);
        response.addHeader("Pragma", "no-cache");
        // Http 1.1 header
        response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");
    }

5、设置客户端缓存过期时间 Header

public static void setExpiresHeader(HttpServletResponse response,
            long expiresSeconds) {
        // Http 1.0 header
        response.setDateHeader("Expires", System.currentTimeMillis()
                + expiresSeconds * 1000);
        // Http 1.1 header
        response.setHeader("Cache-Control", "private, max-age="
                + expiresSeconds);
    }


6、设置客户端无缓存Header

public static void setNoCacheHeader(HttpServletResponse response) {
        // Http 1.0 header
        response.setDateHeader("Expires", 0);
        response.addHeader("Pragma", "no-cache");
        // Http 1.1 header
        response.setHeader("Cache-Control", "no-cache");
    }

7、分析并设置contentType与headers

 public static void initResponseHeader(HttpServletResponse resp,
            String contentType, String... headers) {
        // 分析headers参数
        String encoding = DEFAULT_ENCODING;
        boolean noCache = DEFAULT_NOCACHE;
        for (String header : headers) {
            String headerName = StringUtils.substringBefore(header, ":");
            String headerValue = StringUtils.substringAfter(header, ":");

            if (StringUtils.equalsIgnoreCase(headerName, HEADER_ENCODING)) {
                encoding = headerValue;
            } else if (StringUtils.equalsIgnoreCase(headerName, HEADER_NOCACHE)) {
                noCache = Boolean.parseBoolean(headerValue);
            } else {
                throw new IllegalArgumentException(headerName
                        + "不是一个合法的header类型");
            }
        }
        // 设置headers参数
        String fullContentType = contentType + ";charset=" + encoding;
        resp.setContentType(fullContentType);
        if (noCache) {
            setNoCacheHeader(resp);
        }
    }

8、直接输出内容的简便函数

public static void render(HttpServletResponse resp, String contentType,
            String content, String... headers) {
        initResponseHeader(resp, contentType, headers);
        try {
            resp.getWriter().write(content);
            resp.getWriter().flush();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

9、输出文本

public static void renderText(HttpServletResponse resp, String text,
            String... headers) {
        render(resp, ServletUtils.TEXT_TYPE, text, headers);
    }

10、输出html

public static void renderHtml(HttpServletResponse resp, String html,
            String... headers) {
        render(resp, ServletUtils.HTML_TYPE, html, headers);
    }


11、输出xml

public static void renderXml(HttpServletResponse resp, String xml,
            String... headers) {
        render(resp, ServletUtils.XML_TYPE, xml, headers);
    }

12、输出json

public static void renderJson(HttpServletResponse resp, String jsonString,
            String... headers) {
        render(resp, ServletUtils.TEXT_TYPE, jsonString, headers);
    }


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值