美文网首页
web.xml配置: 禁止游览器对动态内容做缓存

web.xml配置: 禁止游览器对动态内容做缓存

作者: 风亡小窝 | 来源:发表于2017-11-03 11:37 被阅读15次
package com.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class ResponseHeaderFilter implements Filter {
    FilterConfig fc;

    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        // set the provided HTTP response parameters
        Enumeration e = fc.getInitParameterNames();
        while (e.hasMoreElements()) {
            String headerName = (String) e.nextElement();
            response.addHeader(headerName, fc.getInitParameter(headerName));
        }
        // pass the request/response on
        chain.doFilter(req, response);
    }

    public void init(FilterConfig filterConfig) {
        this.fc = filterConfig;
    }

    public void destroy() {
        this.fc = null;
    }

}

在这里以.do结尾的请求均为动态请求
在web.xml中添加

    <filter>
        <filter-name>NoCache</filter-name>
        <filter-class>com.filter.ResponseHeaderFilter</filter-class>
        <init-param>
            <param-name>Cache-Control</param-name>
            <param-value>no-cache</param-value>
        </init-param>
        <init-param>
            <param-name>Pragma</param-name>
            <param-value>No-cache</param-value>
        </init-param>
        <init-param>
            <param-name>Expires</param-name>
            <param-value>0</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>NoCache</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
 ```
http://blog.csdn.net/liujin4049/article/details/3010370

相关文章

网友评论

      本文标题:web.xml配置: 禁止游览器对动态内容做缓存

      本文链接:https://www.haomeiwen.com/subject/scqtmxtx.html