The Method of Transforming Dynamic Web Page into Static Page by jsp

  • 2021-07-09 08:59:31
  • OfStack

In this paper, an example is given to describe the method of transforming dynamic web pages into static pages by jsp. Share it for your reference. The details are as follows:

If I could convert an jsp dynamic page to a static one, I would not need to visit the database frequently.

jsp Display Content Caching Skills

Some time ago, I was a forum for my own community. On the basis of jive, make a page to show all forum posts, which can be called the general version. Imitate the interface of forum to make an superforum and realize cachable. However, because this page refreshes a lot, although it is cache, I still try to cache the page. I feel that the static content of html generated by jsp should be cached, and the page access speed should be improved.

The first way that comes to mind, The urlconnection of java. net is used to catch jsp on the server for caching. But I think it's too strange to do so. Things on your own server, Why use http to access. So think of another way, jsp out object output control to their desired place. For example, output to a static file, or save as a global string variable. In this case, browsing does not need to execute jsp, just browse the html. Only in the data has updated the time to conduct an update operation, jsp re-output to html.

In my opinion, when browsing events occur more often than data insertions or updates. Try this method to improve page access speed.

The whole thing is a bit like using jsp as a template to generate a static html page.

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 cache

The source code of java 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(); 
// Get html  Page result string  
// responsewrapper.writefile(filename); 
// dump the contents  Write as html  Files 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, if resin server is used, the above code will fail. Because resin does not implement the getwriter method, but uses getoutputstream instead, some code must be modified to cater to the resin running 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 is helpful to everyone's JSP programming.


Related articles: