JSP realizes the method that transforms the dynamic webpage into the static page _jsp programming

Source: Internet
Author: User

This article describes the JSP implementation of the Dynamic Web page into a static page method. Share to everyone for your reference. Specifically as follows:

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 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 method, is the use of java.net URLConnection the JSP on the server to catch up to do caching, but I think this is too outsider, the things on their own server, why to use HTTP to access. So think of another way to put the JSP out The output of the object controls where it wants to go. For example, output to a static file, or a string variable that is saved as a global. In this way, browsing does not need to execute a JSP, just browsing the HTML. Just an update when the data is updated, and then output the JSP as 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 > 

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 filterconfig) throws servletexception {Protdirpath = Filterconfig.getservletcontext (). GE 
Trealpath ("/"); public void Dofilter (ServletRequest request,servletresponse response,filterchain chain) throws IOException, Servletex 
ception {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); 
The 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); C26/>fw.write (Output.tochararray ()); 
Fw.close (); 
} 
public void Writeresponse (PrintWriter out) 
{ 
out.print (Output.tochararray ()); 
} 
} 
/** * End File Filecaptureresponsewrapper.java * * 

Attachment Source code:

However, the above code will fail if the resin server is used. Because resin does not implement the Getwriter method, but instead uses Getoutputstream instead, you must modify some code to cater to the resin 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 * *

I hope this article will help you with your JSP programming.

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.