How to convert a JSP Dynamic Web page into a static page __js

Source: Internet
Author: User

If I can convert a JSP dynamic page to a static page, then there is no need to visit the database frequently.

JSP Display content Caching tips
Some time ago to do their own community forum, on the basis of Jive to do a page to display all the forum posts, can be called
For the general edition, imitate the interface of forum class to make 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 cached, the 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 slow
Save, but I think this is too outsider, their own server on the things, why to use HTTP to access. So I thought another
Way to control the output of the JSP's out object to where you want it. For example, output to a static file, or save
A global string variable. In this case, browsing does not need to execute JSP, just browsing the HTML. Only in the data there are more
A new update operation is performed, and the JSP is again exported as HTML.
I think browsing events are more frequent than when data is inserted or updated. Try this to improve the page
Face access speed.
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, and
is replaced with Getoutputstream, so you must 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 * *

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.