Rumah > hujung hadapan web > html tutorial > HTML页面实现全方位页面缓存_html/css_WEB-ITnose

HTML页面实现全方位页面缓存_html/css_WEB-ITnose

WBOY
Lepaskan: 2016-06-24 11:57:55
asal
885 orang telah melayarinya

【1】服务端配置一个Filter,实现对js、css和image的缓存

package cn.com.system.filter;import java.io.IOException;import java.util.Enumeration;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletResponse;public class CacheForWeekFilter {	private FilterConfig fc;	public void doFilter(ServletRequest req, ServletResponse res,			FilterChain chain) throws IOException, ServletException {		HttpServletResponse response = (HttpServletResponse) res;		for (Enumeration e = fc.getInitParameterNames(); e.hasMoreElements();) {			String headerName = (String) e.nextElement();			response.addHeader(headerName, fc.getInitParameter(headerName));		}		chain.doFilter(req, response);	}	public void init(FilterConfig filterConfig) {		this.fc = filterConfig;	}	public void destroy() {		this.fc = null;	}}
Salin selepas log masuk

web.xml配置

<filter>          <filter-name>CacheForWeek</filter-name>          <filter-class>cn.com.system.filter.CacheForWeekFilter</filter-class>          <init-param>              <param-name>Cache-Control</param-name>              <param-value>max-age=604800, public</param-value>          </init-param>      </filter>     <filter-mapping>          <filter-name>CacheForWeek</filter-name>          <url-pattern>/js/</url-pattern>      </filter-mapping>      <filter-mapping>          <filter-name>CacheForWeek</filter-name>          <url-pattern>/images/</url-pattern>      </filter-mapping>      <filter-mapping>          <filter-name>CacheForWeek</filter-name>          <url-pattern>/css/</url-pattern>      </filter-mapping> 
Salin selepas log masuk
完成这一步,在服务端已经实现了对页面的缓存,但是当前情况下页面还是会每次访问服务器的,只是压力减小了

如何让页面在一段时间内不访问服务器呢

实现方式是对应公用的JS都放到一个页面中,别的页面包含他,在这个页面中增加页面缓存

<meta charset="utf-8"><span style="color:#ff0000;"><meta http-equiv="cache-control" content="max-age=604800, public"></span><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><!--IE10--><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"><!--<meta name=”renderer” content=”webkit|ie-comp|ie-stand”>--><meta name="”renderer”" content="”webkit|ie-comp|ie-stand”"><!-- CSS styles --><link rel="stylesheet" type="text/css" href="%24%7BpageContext.request.contextPath%7D/css/base.css"><script src="%24%7BpageContext.request.contextPath%7D/common/jquery/js/jquery-1.8.2.js" charset="utf-8"></script><script src="%24%7BpageContext.request.contextPath%7D/common/jquery/js/jquery.ui.core.js" charset="utf-8"></script>
Salin selepas log masuk



Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan