JSP content caching skills (1)

Source: Internet
Author: User
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 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 */

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.