JSP display content caching skills

Source: Internet
Author: User

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

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.