什么是静态化?静态化就是将动态的jsp转换为静态的html;
为什么要静态化?提高性能加快访问速度,静态化的页面html不需要访问数据库等操作所以要比动态的jsp快很多。
怎样实现静态化?实现静态化的方法很多,本文章要说明的是通过Filter过滤器实现jsp的静态化。
1、我们先了解下jsp页面的请求流程
JSP的静态化 - 程序民工 - suntengjiao1的博客
通过上图我们可以知道servlet对象的响应是html,如果我们在servlet对象之后增加个拦截htm响应的功能将html响应保存到服务器,下次访问时直接返回这个静态页面,也就实现了静态化功能。
2、新的请求流程如下图:
JSP的静态化 - 程序民工 - suntengjiao1的博客
实现上图拦截servlet响应、判断是否已经静态化功能最好的方法就是拦截器Filter。
具体代码如下:
1.过滤器: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: lsj626472785@163.com
* @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;
}
/****************************************** 方式二 ***************************************/
// 得到ServletContext中的"page_map"属性的值,也就是上面说的map集合
Map<String, String> map = (Map<String, String>) context
.getAttribute("page_map");
if (map == null) {
map = new HashMap<String, String>();
context.setAttribute("page_map", map);
}
// 这就是键值
String key = "htmlFile_" + "test";
// 只是value
String htmlPath = map.get(key);
// 为空说明,没有静态页面,不为空直接重定向到该页面
if (htmlPath == null) {
String fileName = "htmlFile" + File.separator + "test.html";
String filePath = protDirPath + fileName;
FileCaptureResponseWrapper responseWrapper = new FileCaptureResponseWrapper(
(HttpServletResponse) response);
chain.doFilter(request, responseWrapper);
// 写成html 文件
responseWrapper.writeFile(filePath);
map.put(key, fileName);
htmlPath = fileName;
}
request.getRequestDispatcher("/" + htmlPath).forward(request, response);
/****************************************** 方式一 ***************************************/
// String fileName = "htmlFile" + File.separator + "test.html";
// File file = new File(context.getRealPath(fileName));
// /*
// * 如果文件存在则直接使用缓存的静态文件,否则保存静态文件再转发
// */
// 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: lsj626472785@163.com
* @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>
为什么要静态化?提高性能加快访问速度,静态化的页面html不需要访问数据库等操作所以要比动态的jsp快很多。
怎样实现静态化?实现静态化的方法很多,本文章要说明的是通过Filter过滤器实现jsp的静态化。
1、我们先了解下jsp页面的请求流程
JSP的静态化 - 程序民工 - suntengjiao1的博客
通过上图我们可以知道servlet对象的响应是html,如果我们在servlet对象之后增加个拦截htm响应的功能将html响应保存到服务器,下次访问时直接返回这个静态页面,也就实现了静态化功能。
2、新的请求流程如下图:
JSP的静态化 - 程序民工 - suntengjiao1的博客
实现上图拦截servlet响应、判断是否已经静态化功能最好的方法就是拦截器Filter。
具体代码如下:
1.过滤器: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: lsj626472785@163.com
* @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;
}
/****************************************** 方式二 ***************************************/
// 得到ServletContext中的"page_map"属性的值,也就是上面说的map集合
Map<String, String> map = (Map<String, String>) context
.getAttribute("page_map");
if (map == null) {
map = new HashMap<String, String>();
context.setAttribute("page_map", map);
}
// 这就是键值
String key = "htmlFile_" + "test";
// 只是value
String htmlPath = map.get(key);
// 为空说明,没有静态页面,不为空直接重定向到该页面
if (htmlPath == null) {
String fileName = "htmlFile" + File.separator + "test.html";
String filePath = protDirPath + fileName;
FileCaptureResponseWrapper responseWrapper = new FileCaptureResponseWrapper(
(HttpServletResponse) response);
chain.doFilter(request, responseWrapper);
// 写成html 文件
responseWrapper.writeFile(filePath);
map.put(key, fileName);
htmlPath = fileName;
}
request.getRequestDispatcher("/" + htmlPath).forward(request, response);
/****************************************** 方式一 ***************************************/
// String fileName = "htmlFile" + File.separator + "test.html";
// File file = new File(context.getRealPath(fileName));
// /*
// * 如果文件存在则直接使用缓存的静态文件,否则保存静态文件再转发
// */
// 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: lsj626472785@163.com
* @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>