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
Some time ago, I made a forum in my own community. On the basis of jive, I made a page to display all Forum posts, which can be called
This is the general version. It imitates the Forum class interface to create a superforum and implement cachable. However, this page
The refresh volume is large. Although it is cached, I still try to cache the page. I feel that the HTML generated by JSP is used.
When static content is cached, the page access speed should be improved.
The first method that comes to mind is to use the urlconnection of java.net to capture the JSP on the server for easing.
But I think it's too easy to do this. Why should I use HTTP to access things on my server?
To control the output of the JSP out object to the desired place. For example, output to a static file or save
To generate a global string variable. In this way, you do not need to execute JSP for browsing, but you only need to browse the HTML.
Perform an update operation at the new time, and output JSP as HTML again.
In my opinion, when Browsing events occur more frequently than data insertion or update events, try this method to improve the page.
Area access speed.
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
Instead of using getoutputstream, 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 */
.