JavaWeb high concurrent access using Filter to achieve page static

What is static? Static is to convert dynamic jsp into static html;
why static? Improve performance and speed up access, static page html does not need to access the database and other operations, so it is much faster than dynamic jsp.
How to achieve static? There are many ways to achieve static. This article will explain the static of jsp through the Filter filter.
1. Let's first understand the request process of jsp pages.

JSP static-program migrant workers- blog of suntengjiao1 From

the above figure, we can know that the response of the servlet object is html. If we add a function to intercept the htm response after the servlet object, the html The response is saved to the server, and the static page is directly returned when the next visit is made, which also realizes the static function.
2. The new request process is as follows:
Staticization of JSP - Program Migrant Workers - blog

of best way to intercept the servlet response and judge whether the function has been static is the interceptor Filter.
The specific code is as follows:
1. Filter: FileCaptureFilter
package com.filter;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Description: 静态化过滤器
* @ClassName: FileCaptureFilter
* @author lsj
* @email: [email protected]
* @date 2016年10月27日 下午5:23:10
*/
public class FileCaptureFilter implements Filter {
private FilterConfig filterConfig;
private String protDirPath;// 项目绝对路径
private ServletContext context;
private String contextPath;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.context = filterConfig.getServletContext();
this.protDirPath = filterConfig.getServletContext().getRealPath("/");
this.contextPath = context.getContextPath();
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8"); // 防止中文乱码
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI();
if (!path.contains("gotoLogin1.do")) {
chain.doFilter(request, response);
return;
}
/******************************************** Method 2**** ************************************/
// Get the value of the "page_map" attribute in the ServletContext, also It is the map collection mentioned above
Map<String, String> map = (Map<String, String>) context
.getAttribute("page_map");
if (map == null) {
map = new HashMap<String, String>() ;
context.setAttribute("page_map", map);
}
// this is the key value
String key = "htmlFile_" + "test";
// just the value
String htmlPath = map.get(key);
// empty description, No static page, not empty redirect to this page directly
if (htmlPath == null) {
String fileName = "htmlFile" + File.separator + "test.html";
String filePath = protDirPath + fileName;
FileCaptureResponseWrapper responseWrapper = new FileCaptureResponseWrapper(
(HttpServletResponse) response);
chain.doFilter(request, responseWrapper);
// write html file
responseWrapper.writeFile(filePath);
map.put(key, fileName);
htmlPath = fileName;
}
request.getRequestDispatcher ("/" + htmlPath).forward(request, response);
/********************************** ******** method one***************************************/
// String fileName = "htmlFile" + File.separator + "test.html";
// File file = new File(context.getRealPath(fileName));
// /*
// * If the file exists, use the cached one directly Static file, otherwise save the static file and forward it
// */
// if (file.exists()) {
// request.getRequestDispatcher("/" + fileName).forward(request,
// response);
// } else {
// String filePath = protDirPath + fileName;
// FileCaptureResponseWrapper responseWrapper = new
// FileCaptureResponseWrapper(
// (HttpServletResponse) response);
// chain.doFilter(request, responseWrapper);
// // 写成html 文件
// responseWrapper.writeFile(filePath);
// // back to browser
// responseWrapper.writeResponse();
// }
}

public void destroy() {


}

}

2.装饰者:FileCaptureResponseWrapper
package com.filter;
import java.io.CharArrayWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* @Description: 静态化处理(装饰者)
* @ClassName: FileCaptureResponseWrapper
* @author lsj
* @email: [email protected]
* @date 2016年10月27日 下午5:39:54
*/
public class FileCaptureResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter output;
private HttpServletResponse response;
private PrintWriter out;

@Override
public String toString() {
return output.toString();
}

public FileCaptureResponseWrapper(HttpServletResponse response) {
super(response);
this.response = response;
output = new CharArrayWriter();
}

// 覆写getWriter()
@Override
public PrintWriter getWriter() {
return new PrintWriter(output);
}


public void writeFile(String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(output.toCharArray());
osw.close();
}


public void writeResponse() throws IOException {
out = response.getWriter();
// 重置响应输出的内容长度
response.setContentLength(-1);
out.print(output.toCharArray());
out.flush();
out.close();
}
public void close(){
out.close();
}
}
3.web.xml配置
<!-- 静态页面拦截器 -->
<filter>
<filter-name>FileCaptureFilter</filter-name>
<filter-class>com.filter.FileCaptureFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FileCaptureFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326189167&siteId=291194637