js| Cache | tips | show
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
<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
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 * *
Attachment Source Code
However, using the resin server, the above code will fail. Because resin did not implement the Getwriter method but instead replaced it with Getoutputstream, you had to modify some code to cater to the resin operating 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 * *