巨人大哥谈Java禁止浏览器有缓存的源码

2018-06-18 02:04:19来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

巨人大哥谈Java禁止浏览器有缓存的源码

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/***
 * 强制不要页面缓存  共同电商学习交流群:168096884  
 * @author*/
public class ForceNoCacheFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(ForceNoCacheFilter.class);

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain)
            throws IOException, ServletException {

        logger.info("[ForceNoCacheFilter] start......");

        HttpServletResponse response = (HttpServletResponse) resp;
        HttpServletRequest request = (HttpServletRequest) req;

        String url = request.getRequestURI();

        logger.info("[ForceNoCacheFilter][web端请求][url]"+url);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", -1);
        filterChain.doFilter(req, resp);
        return;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("[ForceNoCacheFilter] 系统初始化init......");
    }

    @Override
    public void destroy() {
        logger.info("[ForceNoCacheFilter] destroy......");
    }
}

 

同时web.xml不要忘记配置了..

 

<filter>
<filter-name>NoCacheFilter</filter-name>
<filter-class>com.xx.filter.ForceNoCacheFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

 

学习:日积月累,与君共勉.

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring(5)——Spring 和数据库编程

下一篇:HTTP Status 500 - Request processing failed; nested exceptio