zoukankan      html  css  js  c++  java
  • 页面缓存

    http://www.cnblogs.com/liuling/archive/2013/07/25/2013-7-25-01.html

    页面的缓存与不缓存设置

     

      HTML的HTTP协议头信息中控制着页面在几个地方的缓存信息,包括浏览器端,中间缓存服务器端(如:squid等),Web服务器端。本文讨论头信息 中带缓存控制信息的HTML页面(JSP/Servlet生成好出来的也是HTML页面)在中间缓存服务器中的缓存情况。

          HTTP协议中关于缓存的信息头关键字包括Cache-Control(HTTP1.1),Pragma(HTTP1.0),last-Modified,Expires等。

          HTTP1.0中通过Pragma 控制页面缓存,可以设置:Pragma或no-cache。网上有非常多的文章说明如何控制不让浏览器或中间缓存服务器缓存页面,通常设置的值为no- cache,不过这个值不这么保险,通常还加上Expires置为0来达到目的。但是如我们刻意需要浏览器或缓存服务器缓存住我们的页面这个值则要设置为 Pragma。

          HTTP1.1中启用Cache-Control 来控制页面的缓存与否,这里介绍几个常用的参数:

    • no-cache,浏览器和缓存服务器都不应该缓存页面信息;
    • public,浏览器和缓存服务器都可以缓存页面信息;
    • no-store,请求和响应的信息都不应该被存储在对方的磁盘系统中;
    • must-revalidate,对于客户机的每次请求,代理服务器必须想服务器验证缓存是否过时;

           Last-Modified只页面的最后生成时间,GMT格式;

           Expires过时期限值,GMT格式,指浏览器或缓存服务器在该时间点后必须从真正的服务器中获取新的页面信息;

           上面两个值在JSP中设置值为字符型的GMT格式,无法生效,设置long类型才生效;

    下面是一个测试例子:

    复制代码
     1 package com.servlet;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.ServletException;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 public class ServletA extends HttpServlet {
    12     @Override
    13     public void service(HttpServletRequest request, HttpServletResponse response)
    14             throws ServletException, IOException {
    15         response.setContentType("text/html");
    16         //servlet页面默认是不缓存的
    17         //本页面允许在浏览器端或缓存服务器中缓存,时限为20秒。
    18         //20秒之内重新进入该页面的话不会进入该servlet的
    19         java.util.Date date = new java.util.Date();    
    20         response.setDateHeader("Last-Modified",date.getTime()); //Last-Modified:页面的最后生成时间 
    21         response.setDateHeader("Expires",date.getTime()+20000); //Expires:过时期限值 
    22         response.setHeader("Cache-Control", "public"); //Cache-Control来控制页面的缓存与否,public:浏览器和缓存服务器都可以缓存页面信息;
    23         response.setHeader("Pragma", "Pragma"); //Pragma:设置页面是否缓存,为Pragma则缓存,no-cache则不缓存
    24 
    25         //不允许浏览器端或缓存服务器缓存当前页面信息。
    26         /*response.setHeader( "Pragma", "no-cache" );   
    27         response.setDateHeader("Expires", 0);   
    28         response.addHeader( "Cache-Control", "no-cache" );//浏览器和缓存服务器都不应该缓存页面信息
    29         response.addHeader( "Cache-Control", "no-store" );//请求和响应的信息都不应该被存储在对方的磁盘系统中;    
    30         response.addHeader( "Cache-Control", "must-revalidate" );*///于客户机的每次请求,代理服务器必须想服务器验证缓存是否过时;
    31 
    32         System.out.println("进入了servlet");
    33         response.getWriter().write("欢迎光临我的主页");
    34     }
    35 
    36     
    37 }
    复制代码

      如果需要在html页面上设置不缓存,这在<head>标签中加入如下语句:

    1 <meta http-equiv="pragma" content="no-cache">
    2 <meta http-equiv="cache-control" content="no-cache">
    3 <meta http-equiv="expires" content="0">   

    附:html页面中meta的作用

      meta是用来在HTML文档中模拟HTTP协议的响应头报文。meta 标签用于网页的<head>与</head>中,meta 标签的用处很多。meta 的属性有两种:name和http-equiv。name属性主要用于描述网页,对应于content(网页内容),以便于搜索引擎机器人查找、分类(目前几乎所有的搜索引擎都使用网上机器人自动查找meta值来给网页分类)。这其中最重要的是description(站点在搜索引擎上的描述)和keywords(分类关键词),所以应该给每页加一个meta值。比较常用的有以下几个:

      name 属性

      1、<meta name="Generator" contect="">用以说明生成工具(如Microsoft FrontPage 4.0)等;

      2、<meta name="KEYWords" contect="">向搜索引擎说明你的网页的关键词;

      3、<meta name="DEscription" contect="">告诉搜索引擎你的站点的主要内容;

      4、<meta name="Author" contect="你的姓名">告诉搜索引擎你的站点的制作的作者;

      5、<meta name="Robots" contect= "all|none|index|noindex|follow|nofollow">

      其中的属性说明如下:

      设定为all:文件将被检索,且页面上的链接可以被查询;

      设定为none:文件将不被检索,且页面上的链接不可以被查询;

      设定为index:文件将被检索;

      设定为follow:页面上的链接可以被查询;

      设定为noindex:文件将不被检索,但页面上的链接可以被查询;

      设定为nofollow:文件将不被检索,页面上的链接可以被查询。

      http-equiv属性

      1、<meta http-equiv="Content-Type" contect="text/html";charset=gb_2312-80">

    和 <meta http-equiv="Content-Language" contect="zh-CN">用以说明主页制作所使用的文字以及语言;

      又如英文是ISO-8859-1字符集,还有BIG5、utf-8、shift-Jis、Euc、Koi8-2等字符集;

      2、<meta http-equiv="Refresh" contect="n;url=http://yourlink">定时让网页在指定的时间n内,跳转到页面http://yourlink;

      3、<meta http-equiv="Expires" contect="Mon,12 May 2001 00:20:00 GMT">可以用于设定网页的到期时间,一旦过期则必须到服务器上重新调用。需要注意的是必须使用GMT时间格式;

      4、<meta http-equiv="Pragma" contect="no-cache">是用于设定禁止浏览器从本地机的缓存中调阅页面内容,设定后一旦离开网页就无法从Cache中再调出;

      5、<meta http-equiv="set-cookie" contect="Mon,12 May 2001 00:20:00 GMT">cookie设定,如果网页过期,存盘的cookie将被删除。需要注意的也是必须使用GMT时间格式;

      6、<meta http-equiv="Pics-label" contect="">网页等级评定,在IE的internet选项中有一项内容设置,可以防止浏览一些受限制的网站,而网站的限制级别就是通过meta属性来设置的;

      7、<meta http-equiv="windows-Target" contect="_top">强制页面在当前窗口中以独立页面显示,可以防止自己的网页被别人当作一个frame页调用;

      8、<meta http-equiv="Page-Enter" contect="revealTrans(duration=10,transtion= 50)">和<meta http-equiv="Page-Exit" contect="revealTrans(duration=20,transtion=6)">设定进入和离开页面时的特殊效果,这个功能即FrontPage中的“格式/网页过渡”,不过所加的页面不能够是一个frame页面。

    我喜欢,驾驭着代码在风驰电掣中创造完美!我喜欢,操纵着代码在随必所欲中体验生活!我喜欢,书写着代码在时代浪潮中完成经典!每一段新的代码在我手中诞生对我来说就象观看刹那花开的感动!
     
    SpringMVC设置静态页面缓存:http://hc24.iteye.com/blog/2063660
    dispatcher-servlet.xml:
      
    1. <!-- 处理静态资源 -->  
    2.     <!-- 上传的图片缓存1个月,其他js,css,img资源缓存一年 -->  
    3.     <mvc:resources mapping="/res/**" location="/res/" cache-period="2592000"/>   
    4.     <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31536000"/>   
    5.     <mvc:resources mapping="/css/**" location="/css/" cache-period="31536000"/>  
    6.     <mvc:resources mapping="/js/**" location="/js/" cache-period="31536000"/>  
    7.     <mvc:resources mapping="/img/**" location="/img/" cache-period="31536000"/>  
    8.     <mvc:resources mapping="/images/**" location="/images/" cache-period="31536000"/> 
     
     
  • 相关阅读:
    数组
    Spring创建对象的三种方式以及创建时间
    Struts文件上传下载
    自定义拦截器
    Struts过滤器
    mybatis整合ehcache
    mybatis主键返回
    shell脚本 列出所有网卡的ip地址
    Servlet执行过程
    centos时区
  • 原文地址:https://www.cnblogs.com/shoubianxingchen/p/6673409.html
Copyright © 2011-2022 走看看