This article describes the method of converting dynamic web pages into static pages in JSP. Share it for your reference. The details are as follows:
If I can convert jsp dynamic web pages into static pages, I don’t need to access the database frequently when accessing.
jsp display content caching tips
Some time ago, I started my own community forum and made a page to display all the forum posts on jive. It can be called the general version. I imitated the interface of the forum class to make a superforum and implement cacheable. However, because the page refresh volume is relatively large, although it was cached, I still found a way to cache the page. I feel that using html static content generated by jsp as cache, the page access speed should be improved.
The first way I thought of is to use urlconnection to capture the jsp on the server for cache, but I think this is too exaggerated. Why do things on my server use http to access them? So I thought of another way to control the output of the jsp out object to where I want it. For example, output to a static file or save it as a global string variable. In this way, you do not need to perform jsp on browsing, just browse the html. Just perform an update operation when the data is updated, and re-output jsp into html.
I think browsing events occur more than data insertion or updates. You might as well try this method to improve page access speed.
The whole thing is a bit like treating jsp as a template to generate a static html page.
Write the following code to web-xml:
<filter> <filter-name>filecapturefilter</filter-name> <filter-class></filter-class> </filter> <filter-mapping> <filter-name>filecapturefilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping>
It's the page I want to cache
The java source code is as follows:
/** * start file */ package ; import .*; import .*; import .*; public class filecapturefilter implements filter { private string protdirpath; public void init(filterconfig filterconfig) throws servletexception { protdirpath = ().getrealpath("/"); } public void dofilter(servletrequest request,servletresponse response,filterchain chain) throws ioexception, servletexception { string filename = protdirpath + "forum/"; printwriter out = (); filecaptureresponsewrapper responsewrapper = new filecaptureresponsewrapper((httpservletresponse)response); (request, responsewrapper); // fill responsewrapper up string html = (); //The result string of the html page// (filename); // dump the contents is written as an html file, and can also be saved in memory//( out ); // back to browser //(""); } public void destroy() {} } /** * end file */
/** * start file */ package ; import .*; import .*; import .*; public class filecaptureresponsewrapper extends httpservletresponsewrapper { private chararraywriter output; public string tostring() { return (); } 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); ( () ); (); } public void writeresponse(printwriter out) { ( () ); } } /** * end file */
Attachment source code:
However, if the resin server is used, the above code will be invalid. Because resin does not implement the getwriter method, but uses getoutputstream instead, it must modify some code to cater to the resin running environment:
/** * start file */ package ; import .*; import .*; import .*; public class filecaptureresponsewrapper extends httpservletresponsewrapper { private chararraywriter output; public string tostring() { return (); } 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); ( ()); (); } public servletoutputstream getoutputstream() throws { return new servletoutputstream(); } public void write(int b) throws ioexception { (b); } public void write(byte b[]) throws ioexception { (new string(b,"gbk")); } public void write(byte b[], int off, int len) throws ioexception { (new string(b, off, len)); } }; } public void writeresponse(printwriter out) { (()); } } /** * end file */
I hope this article will be helpful to everyone's JSP programming.