HTML页面实现全方位页面缓存

当前位置 : 首页 > 网页制作 > CSS > HTML页面实现全方位页面缓存

HTML页面实现全方位页面缓存

来源: 作者: 时间:2016-01-23 10:51
【1】服务端配置一个Filter,实现对js、css和image的缓存package cn com system filter;import java io IOException;import java util Enumeration;import javax servlet FilterChain;import javax

【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;
	}
}

web.xml配置

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

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

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

<%@ page language="java" pageEncoding="UTF-8"%>












<script src="${pageContext.request.contextPath}/common/jquery/js/jquery-1.8.2.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/common/jquery/js/jquery.ui.core.js" charset="utf-8"></script>



Tag:
网友评论

<