Jsp implements the method of converting dynamic web pages into static pages. jsp static pages

Source: Internet
Author: User

Jsp implements the method of converting dynamic web pages into static pages. jsp static pages

This example describes how to convert a dynamic web page to a static page through jsp. Share it with you for your reference. The details are as follows:

If I can convert a dynamic jsp page to a static page, I do not need to access the database frequently.

Jsp display content caching skills

I worked as a forum in my own community some time ago. I made a page on the basis of jive to display all the forum posts, which can be called the general edition. I used the forum class interface to create a superforum and implement cachable, however, because the page is refreshed a lot, even if it is cached, I still want to cache the page. I feel that the html static content generated by jsp is used as the cache, the page access speed should be improved.

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 (); // The html page result string // responsewrapper. writefile (filename); // dump the contents into an html file, 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 */ 

Source code of the Attachment:

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 */

I hope this article will help you with JSP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.