Home >Web Front-end >HTML Tutorial >HTML page realizes comprehensive page caching_html/css_WEB-ITnose
【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; }}
<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>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>