How to control cache in JSP page?

前端 未结 4 1607
温柔的废话
温柔的废话 2020-12-16 06:44

I created a Servlet filter with the following code in doFilter:

HttpServletResponse httpResponse = (HttpServletResponse)response;

httpResponse.setHeader(\"C         


        
相关标签:
4条回答
  • 2020-12-16 07:13

    We also use the Last-Modified header to encourage the browser to always get a new version.

    setDateHeader("Last-Modified", (new Date()).getTime() ); // Set last modified to right now.

    I'm not sure about keeping the browser's back button from using the cache, though. Anything that you do with header fields is only going to be evaluated when the page is first loaded, IIRC.

    0 讨论(0)
  • 2020-12-16 07:14

    To start, the complete set is:

    httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    httpResponse.setDateHeader("Expires", 0); // Proxies.
    

    The no-store and must-revalidate are required to get it to work in under each Firefox.

    But, even after implementing the above filter, some pages are cached (accessible using browser's back button).

    How did you test it? Those headers will actually prevent the browser from requesting the page from the browser cache instead of directly from the server. Best test is to have a Filter to listen on /* and add a debug statement in flavor of:

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String method = httpRequest.getMethod();
    String URI = httpRequest.getRequestURI();
    System.out.println(method + " request invoked on " + URI);
    

    This should print the actual requests.

    Also ensure that you don't override the headers in the JSP page itself using the HTML <meta> tags.

    And other pages that are not cached, show Web Page Expired error in Internet Explorer.

    You can only get this if the non-cached request was POST request, not a GET request. The GET requests will simply be requested from server again instead of from the browser cache.

    0 讨论(0)
  • 2020-12-16 07:18

    I found this in the oracle docs where they mention that you should never use

    httpResponse.setDateHeader("Expires", 0);
    

    instead of it you should use

    setDateHeader("Last-Modified", (new Date()).getTime() );
    

    http://docs.oracle.com/cd/E13158_01/alui/wci/docs103/devguide/tsk_pagelets_settingcaching_httpexpires.html

    0 讨论(0)
  • 2020-12-16 07:28

    I have found this info: http://docs.oracle.com/cd/E19501-01/819-3659/beagw/index.html (see The cache Tag)

    0 讨论(0)
提交回复
热议问题