Tomcat: Cache-Control

前端 未结 6 710
悲哀的现实
悲哀的现实 2020-11-30 19:19

Jetty has a CacheControl parameter (can be specified webdefault.xml) that determines the caching behavior of clients (by affecting headers sent to clients).

Does Tom

6条回答
  •  难免孤独
    2020-11-30 20:03

    Similar to the post above, except there are some issues with that code. This will disable all browser caching:

    import javax.servlet.*;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Date;
    
    public class CacheControlFilter implements Filter {
    
        public void doFilter(ServletRequest request, ServletResponse response,
                             FilterChain chain) throws IOException, ServletException {
    
            HttpServletResponse resp = (HttpServletResponse) response;
            resp.setHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT");
            resp.setDateHeader("Last-Modified", new Date().getTime());
            resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
            resp.setHeader("Pragma", "no-cache");
    
            chain.doFilter(request, response);
        }
    
    }
    

    and then map in web.xml as described in Stu Thompson's answer.

提交回复
热议问题