public class StateHtmlFilter extends OncePerRequestFilter {
private boolean debug=true;
String stres="";
private FilterConfig filterConfig=null;
public void StateHtmlFilter(FilterConfig filterConfig){
this.filterConfig=filterConfig;
}
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
// 请求的uri
String uri = request.getRequestURI();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// uri中包含background时才进行过滤
HttpServletResponse httpRes=(HttpServletResponse) response;
String path=((HttpServletRequest)request).getServletPath();
///stres="html"+path.substring(4);
stres= path.substring(1); //获取文件名
String pString=request.getSession().getServletContext().getRealPath("/");//
if (uri.indexOf("jsp") != -1) {
ResponseWrapper responseWrapper=new ResponseWrapper(httpRes); //创建自定义的应答对象
chain.doFilter(request,responseWrapper); //完成过滤连的业务
responseWrapper.getWriter().flush();
int len=stres.length(); //获取文件名的长度
String strs=stres.substring(0,len-4)+".html"; //定义静态文件的名称
String jspPath=request.getRealPath(stres); //获取文件的真实存储路径
File jspFile=new File(jspPath); //创建index.jsp文件对象
//String static_path=jspFile.getParent() ;
File htmlFile=new File(pString+"/html/",strs.substring(4)); //定义静态文件的存储路径
Date htmlDate=null;
Date now=new Date();
if (htmlFile.exists()) {
htmlDate=new Date(htmlFile.lastModified());
}else{
htmlFile.createNewFile();
}
if(debug || htmlDate==null || htmlDate.getDate()!=now.getDate()){
FileOutputStream fileStream=new FileOutputStream(htmlFile);//创建HTML文件的输出流
DataOutputStream fout=new DataOutputStream(fileStream); //创建数据输出流
DateFormat dateFormat=DateFormat.getDateTimeInstance(); //创建日期
fout.writeChars(""); //输出空字符
// fout.writeUTF("生成时间:"+dateFormat.format(new Date()));
fout.writeUTF(responseWrapper.getContent()); //使用UTF格式输出HTML内容,保存到.html文件中
fout.close();
}
System.out.println("/html/"+strs.substring(4)+"");
request.getRequestDispatcher("/html/"+strs.substring(4)+"").forward(request, response);
}
//如果已经生成HTML,直接把请求转发到html文件。
else{
System.out.println(""+stres+"");
request.getRequestDispatcher("/"+stres+"").forward(request, response);
}
}
public void destroy(){
}
public void log(String msg){
filterConfig.getServletContext().log(msg);
}
private boolean debug=true;
String stres="";
private FilterConfig filterConfig=null;
public void StateHtmlFilter(FilterConfig filterConfig){
this.filterConfig=filterConfig;
}
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
// 请求的uri
String uri = request.getRequestURI();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// uri中包含background时才进行过滤
HttpServletResponse httpRes=(HttpServletResponse) response;
String path=((HttpServletRequest)request).getServletPath();
///stres="html"+path.substring(4);
stres= path.substring(1); //获取文件名
String pString=request.getSession().getServletContext().getRealPath("/");//
if (uri.indexOf("jsp") != -1) {
ResponseWrapper responseWrapper=new ResponseWrapper(httpRes); //创建自定义的应答对象
chain.doFilter(request,responseWrapper); //完成过滤连的业务
responseWrapper.getWriter().flush();
int len=stres.length(); //获取文件名的长度
String strs=stres.substring(0,len-4)+".html"; //定义静态文件的名称
String jspPath=request.getRealPath(stres); //获取文件的真实存储路径
File jspFile=new File(jspPath); //创建index.jsp文件对象
//String static_path=jspFile.getParent() ;
File htmlFile=new File(pString+"/html/",strs.substring(4)); //定义静态文件的存储路径
Date htmlDate=null;
Date now=new Date();
if (htmlFile.exists()) {
htmlDate=new Date(htmlFile.lastModified());
}else{
htmlFile.createNewFile();
}
if(debug || htmlDate==null || htmlDate.getDate()!=now.getDate()){
FileOutputStream fileStream=new FileOutputStream(htmlFile);//创建HTML文件的输出流
DataOutputStream fout=new DataOutputStream(fileStream); //创建数据输出流
DateFormat dateFormat=DateFormat.getDateTimeInstance(); //创建日期
fout.writeChars(""); //输出空字符
// fout.writeUTF("生成时间:"+dateFormat.format(new Date()));
fout.writeUTF(responseWrapper.getContent()); //使用UTF格式输出HTML内容,保存到.html文件中
fout.close();
}
System.out.println("/html/"+strs.substring(4)+"");
request.getRequestDispatcher("/html/"+strs.substring(4)+"").forward(request, response);
}
//如果已经生成HTML,直接把请求转发到html文件。
else{
System.out.println(""+stres+"");
request.getRequestDispatcher("/"+stres+"").forward(request, response);
}
}
public void destroy(){
}
public void log(String msg){
filterConfig.getServletContext().log(msg);
}
}
项目目录结构是:
jsp文件夹里放置 list1.jsp 页面
再创建个html文件夹 放置上面生成的静态页面 list1.html
访问路径是先走: http://localhost:8090/test/jsp/list1.jsp
访问静态页面路径:http://localhost:8090/test/html/list1.html
达到静态化页面。。