Cache JSP displayed HTML

zhaozj2021-02-11  160

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 the page cache, I feel that the HTML static content generated by JSP is a cache. The page access speed should be improved. First I think of the way, using java.net URLConnection put the server on the server JSP caught up to make a cache, but I think so too, what you have to access it on your server. So I use HTTP to access. So I think another way, control the output of the OUT object of JSP to the place where I hope. For example, output to static files, or save a global string variable. In this way, browsing does not need to perform JSP, just browse the HTML. Just perform an update operation when the data is updated, and re-output the JSP 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 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 saved //ResponseWrapper.WriteSponse (out); // back to browser /// ResponseWrapper.sendredire CT ("Lastthread.jsp");} public void destroy () {}} / ** * end file file file filefilter.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 CharArrayWriter ();} public PrintWriter getWriter () {return new PrintWriter (output);} public void writeFile (String fileName) throws IOException {FileWriter fw = new FileWriter (fileName); fw.write (output.toCharArray ()); fw.close ();} public void writeResponse (PrintWriter out) {out.print (output. TOCHARRAY ());}} / ** * end file file FileCaptureresponseWrapper.java * / Accessories Source code

However, the above code will fail with the RESIN server. Because Resin does not implement the getWriter method, it is replaced with getOutputStream, so you must modify some code to cater to the RESIN runtime: / ** * start file file fileupTureresponseWrapper.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 CharArrayWriter ();} public PrintWriter getWriter () {return new PrintWriter (output);} public void writeFile (String fileName) throws IOException {FileWriter fw = new FileWriter (fileName); fw.write (output.toString ( )); fw.close ();} public ServletOutputStream getOutputStream () throws java.io.IOException {return new ServletOutputStream () {public void write (int b) throws IOException {output.write (b);} public void write ( BYTE B []) THROWS IOEXCEPTION {Output.write (New ST Ring (B, "GBK"));} public void write (byte b [】, int LEN) throws ioException {Output.write (new string (b, off, len));}}; Writeresponse (PrintWriter out) {out.print (output.tochararray ());}} / ** * end file file file fileuptureresponsewrapper.java * /

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

New Post(0)