您的位置:首页 > Web前端 > JavaScript

Page caching discussion

2004-11-15 13:18 417 查看
Fixing problems with JSP and caching proxy server
 
Many of the proxy servers in use today are caching servers. These servers tend to recognize cgi, asp, and cfm as dynamic. They then always request a new page from the server rather than giving their client the page from the proxy cache. The problem is that most of these proxies do not recognize jsp as dynamic. To make matters worse, if the proxy server asks for the last modified date from the jsp page, it gets the compile date.
There is a simple fix for this. Add the following to your JSP pages where you have this problem.

%!
return current time to proxy
4000
server request
public long getLastModified(HttpServletRequest request) {
 return System.currentTimeMillis();
}
%

To ensure that the web-browser itself will always obtain the latest version of the page, the following can be added to the beginning of your JSP (before any content has been committed).

response.setHeader( Cache-Control, no-cache );
response.setHeader( Pragma, no-cache );
response.setIntHeader( Expires, 0 );
------------------------------------------------------------------------------
Comment:
i have used this tags in my jsp page where i m using post method.
%response.setHeader( Cache-Control, no-cache ); response.setHeader( Pragma, no-cache ); response.setIntHeader(Expires, 0 ); %

Now the problem is whenever i submit my form and then go back & forward it gives me this warning.......so i m not able to view my page.....

Warning Page has Expired The page you requested was created using information you submitted in a form. This page is no longer available. As a security precaution, Internet Explorer does not automatically resubmit your information for you. To resubmit your information and view this Web page, click the Refresh button.
------------------------------------------------------------------------------
You can stop pages caching their data...  It's a pain (as there are buggy browsers out there), but this works for us:

<%
        // Set to expire far in the past.
        response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");

        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");

        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
%>
------------------------------------------------------------------------------
You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser.

You need both the statements to take care of some of the older browser versions.
<%
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
If the above fails, try changing the first line to
response.setHeader("Cache-Control","no-store"); //HTTP 1.1

The difference between no-cache and no-store is a bit dodgy, but apparently no-cache is the more polite keyword. However, please note that there are some problems with disabling page caching under IE 5.0 due to the unique buffering requirements of the browser. Please see the Microsoft knowledge base for details:

http://support.microsoft.com/support/kb/articles/Q222/0/64.ASP
http://support.microsoft.com/support/kb/articles/Q234/2/47.ASP

i added this code to the page.

<meta http-equiv="expires" content=<%= new java.util.Date() %>>
<meta http-equiv="no-cache">

added within the head tags.it worked for all browsers.
<%
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1);
%>

I am also doing this at the very bottom of the JSP file:

</body>  <!-- end of the content body -->
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息