Home > Web Front-end > HTML Tutorial > HTML page realizes comprehensive page caching_html/css_WEB-ITnose

HTML page realizes comprehensive page caching_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:57:55
Original
886 people have browsed it

【1】Configure a Filter on the server to implement caching of js, css and 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;	}}
Copy after login

web.xml configuration

<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> 
Copy after login
After completing this step, the page caching has been implemented on the server side. However, under the current situation, the page will still access the server every time, but the pressure is reduced

How to prevent the page from accessing the server for a period of time

The implementation method is to put all the corresponding public JS into one page, other pages include it, and add page cache to this page

<%@ page language="java" pageEncoding="UTF-8"%><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="${pageContext.request.contextPath}/css/base.css" /><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>
Copy after login



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template