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, although it is cached, I still want to find a way to Cache the page. I feel like using jsp (the preferred choice for SUN Enterprise applications) when the generated html static content is cached, 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 (the preferred choice for SUN Enterprise Applications) for caching. However, I think this is too easy to understand, why should I use HTTP to access things on my own server. so I want to find another way to control the output of the out object of jsp (the first choice for SUN Enterprise applications) to what I want. 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 (the first choice for SUN enterprise-level applications), but you just need to browse the html. only one update operation is performed when data is updated, and jsp (the preferred choice for SUN Enterprise Applications) is output as html.
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 (the first choice for SUN Enterprise Applications) as a template to generate static html pages.
Write the following code into web-xml (standardization is getting closer and closer)
<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 (preferred for SUN Enterprise Applications) </url-pattern>
</Filter-mapping>
Latest. jsp (first choice for SUN Enterprise Applications) 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 (preferred for SUN Enterprise Applications )");
}
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 ()