JSP display content cache skills (1)

xiaoxiao2021-03-06  64

Time to do your own community forum, on the basis of jive, make a page to display all forums, can be called a total version, imitate the interface of the Forum class to make a superforum and implement Cachable, but because this page is more refreshed. Although it is cache, I still want to make a way to make a cache, feeling the HTML static content generated by JSP as a cache, the page access speed should be improved.

First, I think that the URLConnection of Java.Net will make the JSP on the server to make a cache, but I think so too much, what I have to access it on my server, why do I use http to access. So I think another One way, control the output of the OUT object of JSP to the place you want. For example, output to a static file, or save into a global string variable. In this way, browsing does not need to perform JSP, just browse the HTML. Just When the data is updated, the UPDATE operation is performed, and the JSP is re-output to HTML.

I think that the browsing event is more than the number of times the data is inserted or updated. Try this method to increase the page access speed.

The whole thing is a bit like a template to generate a static HTML page like a template.

Write the following code to WEB-XML

FileCaptureFilter com.junjing.filter.FileCaptureFilter FileCaptureFilter /latest.jsp

Latest.jsp is the page Java source code I want to cache as follows

/ ** * START File FileCaptureFilter.java * / package com.junjing.filter; import javax.servlet *;. Import javax.servlet.http *;. Import java.io *;. Public class FileCaptureFilter implements Filter {private String protDirPath ; public void init (FilterConfig filterConfig) throws ServletException {protDirPath = filterConfig.getServletContext () getRealPath ( "/");.} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {String fileName = protDirPath "forum / lastest.html"; PrintWriter out = response.getWriter (); FileCaptureResponseWrapper responseWrapper = new FileCaptureResponseWrapper ((HttpServletResponse) response); chain.doFilter (request, responseWrapper); // fill responseWrapper up String html = responseWrapper.toString ( ); // The resulting HTML page result string // responseWrapper.WriteFile (filename); // Dump the contents Write into an HTML file, or save //Responsewrapper.writeresponse (out); // back to browser /// ResponseWrapper.sendredir ect ( "lastestThread.jsp");} public void destroy () {}} / ** * END File FileCaptureFilter.java * // ** * START File FileCaptureResponseWrapper.java * / package com.junjing.filter; import javax. servlet *;. import javax.servlet.http *;. import java.io *;. public class FileCaptureResponseWrapper extends HttpServletResponseWrapper {private CharArrayWriter output; public String toString () {return output.toString ();} public FileCaptureResponseWrapper (HttpServletResponse response) {Super (response); output = new charRayWriter ();} public printwriter getWriter () {Return New PrintWriter (OUTPUT)

转载请注明原文地址:https://www.9cbs.com/read-86166.html

New Post(0)