JSP page caching cache technology introduction and implementation of browser caching

  • 2020-05-17 06:08:30
  • OfStack

1. An overview of the
The idea of caching can be applied at all levels of the software hierarchy. It is an internal mechanism, to the outside world, is imperceptible.
The database itself has a cache, and the persistence layer can also cache. (for example: hibernate, there are also 1 and 2 levels of caching)
The business layer can also have caching (but in general, this is a procedure area and no caching is set).
The presentation/data services layer (the traditional web presentation layer) can also set up the cache (jsp cache is this layer and implements the caching mechanism on app server)
In addition, Browser also has caching (IE), which is also known to all (the caching mechanism implemented on web server). The higher the cache effect, the better, and the lower the cache effect, the more far-reaching.

2. Cache implementation (the browser caches the currently accessed JSP dynamic pages)
(1) server-side method:
 
< %  
response.setHeader("Pragma","No-cache"); 
response.setHeader("Cache-Control","no-cache"); 
response.setDateHeader("Expires", -10); 
 % > 

(2) client method:
meta is used to simulate the response header message of HTTP protocol in HTML documents. The meta tag is used in the < head > and < /head > of web pages, while the meta tag has many USES. There are two types of meta properties: name and http-equiv. The name attribute is mainly used to describe web pages, corresponding to content (web content), so as to facilitate search engine robots to search and categorize web pages (at present, almost all search engines use online robots to automatically find meta values to categorize web pages). The most important of these are description (the description of the site on the search engine) and keywords (the classified keywords), so you should add an meta value to each page. More commonly used are the following:
name properties
1. < meta name="Generator" contect="" > "to illustrate the generation tool (e.g. Microsoft FrontPage 4.0);
2, < meta name="KEYWords" contect=""
3. < meta name="DEscription" contect="" > tells the search engines the main content of your site;
4. < meta name="Author" contect=" your name "> tells the search engine who made your site;
5, < meta name="Robots" contect="all|none|index|noindex|follow|nofollow" >
The properties are explained as follows:
Set to all: the file will be retrieved and the link on the page can be queried.
Set to none: the file will not be retrieved and the link on the page cannot be queried;
Set to index: the file will be retrieved;
follow: links on the page can be queried;
noindex: files will not be retrieved, but links on the page can be queried.
Set to nofollow: files will not be retrieved, links on the page can be queried.
http - equiv properties
1. < meta http-equiv =" Content-Type "contect="text/html"; charset = gb_2312-80 ">
And < meta http-equiv =" es1064en-Language "contect=" zh-CN" > to explain the text and language used to make the homepage; Another example is the ISO-8859-1 character set in English, as well as the BIG5, utf-8, shift-Jis, Euc and Koi8-2 character sets.
2. < meta http-equiv ="Refresh" contect="n "; url=http://yourlink" > allows the web page to jump to http at a specified time within n; / / yourlink;
< meta http-equiv ="Expires" contect="Mon,12 May 2001 00:20:00 GMT" > can be used to set the expiration time of a web page. Note that you must use the GMT time format;
4. < meta http-equiv ="Pragma" contect=" no-cache "> is used to prevent the browser from retrieving the page contents from the local machine's cache.
5, < meta http-equiv =" set-cookie "contect="Mon,12 May 2001 00:20:00 GMT" > cookie setting, if the page expires, the cookie saved will be deleted. Note that you must also use the GMT time format;
6, < meta http-equiv =" Pics-label "contect="" > page rating", in the IE internet option has a content setting, can prevent browsing 1 restricted sites, and the site limit level is set through the meta property;
7. < meta http-equiv =" windows-Target "contect="_top" > forces the page to be displayed as a separate page in the current window, which can prevent your page from being called as one frame page by others;
8, < meta http-equiv =" Page-Enter "contect="revealTrans(duration=10,transtion= 50)" > and < meta http-equiv =" Page-Exit" contect="revealTrans(duration=20, transtion =6)" > This is known as the "format/page transition" in FrontPage, but you cannot add a single frame page.
3. Cache application
(1) prevent JSP page cache in order to prevent the browser from caching the currently accessed JSP dynamic page, you can set it as follows:
< %
// set the expiration date to 1 past time
response.setHeader("Expires", "Sat, 6 May 199512:00:00 GMT");
// set HTTP/1.1 no-cache header
response.setHeader("Cache-Control", "no-store,no-cache,must-revalidate");
// set IE extension HTTP/1.1 no-cache headers, add by user
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// set the standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
% >
Of course, it would be tedious to include this code on every page, and you can use a custom filter (Filter) to handle the relevant pages
(2) jsp,html clear the page cache
1. Disable client cache to be used in < head > Similar contents are added as follows:
 
<META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> 
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 199708:21:57 GMT"> 

or
 
<meta http-equiv="pragma" content="no-cache"> 
<meta http-equiv="cache-control" content="no-cache"> 
<meta http-equiv="expires" content="0"> 

2. Disable caching in the dynamic web page of the server, and add the following script
 
response.setHeader("Pragma","No-cache"); 
response.setHeader("Cache-Control","no-cache"); 
response.setDateHeader("Expires", 0); 

(3) set the cache for limited time
 
int minutes = 10; 
Date d = new Date(); 
String modDate = d.toGMTString(); 
String expDate = null; 
expDate = (new Date(d.getTime() + minutes * 60000)).toGMTString(); 
response.setHeader("Last-Modified", modDate); 
response.setHeader("Expires", expDate); 
response.setHeader("Cache-Control", "public"); // HTTP/1.1 
response.setHeader("Pragma", "Pragma"); // HTTP/1.0 

Supplementary: a few Suggestions on.jsp cache:
1.jsp cache is best done on a filter, where all pages that need to be buffered are grouped in the same directory. It is convenient to change web.xml every time you make a change.
2.Gzip compression reduces page compression to a minimum, with an average compression ratio of 1/3. jsp cache HashMap buffering the compressed pages will definitely save memory consumption and be more efficient than before.

Related articles: