JSP Display content Caching tips

Source: Internet
Author: User
Tags filter html page

Some time ago to do their own community forum, on the basis of Jive to do a page to show all the forum posts, can be called the general version, imitate the Forum class interface to do a superforum and realize cachable, but because this page refresh volume is larger, although the cache, I still think of ways to cache the page, the feeling of using JSP generated HTML static content when caching, page access speed should be improved. First thought of a way, is to use the java.net URLConnection the JSP on the server to catch up to do caching, but I think this is too outsider, their own server on the things, Why do you want to access it with HTTP? And then think of another way to control the output of the JSP out object to where you want it. For example, to output to a static file, or to a string variable that is saved as a global. In this way, browsing does not require the implementation of JSP, Just browsing the HTML. Only update the data when it is updated, and then export the JSP back to HTML.

I think browsing events are more frequent than data inserts or updates. Try this to improve the speed of page access.

The whole thing is a bit like taking a JSP as a template and generating static HTML pages.

Write the following code to Web-xml

Filecapturefilter

Com.junjing.filter.FileCaptureFilter

Filecapturefilter

/latest.jsp

Latest.jsp is the page I want to cache

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 ();

The resulting HTML page result string

Responsewrapper.writefile (FileName);

Dump the contents is written as an HTML file and can also be saved in 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.