Use Filter to specify your browser to cache or not cache server data
At www.jdon.com, Mr. Peng said that the cache design is the key to improving the performance of Java system. The reasonable application of the cache is the most convincing place in the Jive Forum. Below we use the definition of the request head type Cache-Control in the HTTP Specification 1.1 to explain how to use the Filter mechanism to implement HTTP requests or not cache. Here we are listed here is to cache a picture, such as a website logo. We can specify the client browser cache to this logo cache, or not cache, and how long is a cache. In fact, similar functions are also available in ASP.NET, and more simple, interested readers can see the related articles of ASP.NET. Below we use the Servlet's Filter mechanism to implement the above features. The filter code is as follows: import java.io. *; import javax.servlet *; import javax.servlet.http *; import java.util *; public class ResponseHeaderFilter implements Filter {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;}} Add the following configuration
Pay attention to the black body part of the above. We put the above code in Tomcat (5.0.16) for testing. Note: In order to record the log requested by Tomcat, you need to remove the
Some text, with a logo p> < Img src = "logo.png"> body> html> Put Logo.png in the appropriate directory: 2004-01-15 18:48:20 RequestDumperValve [Catalina]: Request URI = / TEST. HTML ... (first visited) ======================================= ======================================= 2004-01-15 18:48:21 RequestDumperValve [Catalina]: header = cache-control = max-agn = 3600 ---------- ... (non-first access) == ============================================================================================================================================================================================================= =========== 2004-01-15 18:48:20 RequestDumperValVe [Catalina]: Request URI = / Test.html ---------- Vis, logo.png in cache During the period, it was not outputted by the server.
According to HTTP1.1 specification, there are two ways to make browser cache data: 1. if-modified-since http request header 2, cache-control is of course if the data can be used as the following configuration:
Accesine@163.com
Author column
http://www.9cbs.net/develop/author/netauthor/accesine960/
Data flow to the client through the data stream in JSP