One of the first methods is to use the urlconnection of java.net to capture the JSP on the server for caching. However, I think this is too easy to understand, why access through HTTP. so I thought of another way to control the output of the JSP out object to the desired place. for example, output to a static file or save it as a global string variable. in this case, you do not need to execute JSP for browsing, but you just need to browse the HTML. only one update operation is performed when data is updated, and JSP is output as HTML again.
In my opinion, when Browsing events occur more frequently than data insertion or update events, try this method to speed up page access.
The whole thing is a bit like using JSP as a template to generate static html pages.
Write the following code into web-XML
<Filter> <Filter-Name> filecapturefilter </filter-Name> <Filter-class> com. junjing. Filter. filecapturefilter </filter-class> </Filter><Filter-mapping> <Filter-Name> filecapturefilter </filter-Name> <URL-pattern>/latest. jsp </url-pattern> </Filter-mapping> |
Latest. jsp is the page I Want To Cache
The Java source code is 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) 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 (); // Obtain the HTML page result string // Responsewrapper. writefile (filename ); // Dump the contents into HTML files, which can also be stored in the memory // Responsewrapper. writeresponse (out ); // Back to Browser // Responsewrapper. sendredirect ("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 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. tochararray ()); } } /*** End file filecaptureresponsewrapper. Java */ |
Attachment source code
However, if the Resin server is used, the above Code will become invalid. Because resin does not implement the getwriter method but uses getoutputstream instead, you must modify some code to cater to the resin runtime environment:
/*** 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. 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 string (B, "GBK ")); } Public void write (byte B [], int off, int Len) Throws ioexception { Output. Write (new string (B, off, Len )); } }; } Public void writeresponse (printwriter out) { Out. Print (output. tochararray ()); } } /*** End file filecaptureresponsewrapper. Java */ |