死磕基础系列之自定义静态化页面的过滤器
Posted JAVA经典代码示例
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了死磕基础系列之自定义静态化页面的过滤器相关的知识,希望对你有一定的参考价值。
直接接昨天的两个工具类,本实例可以进行优化。
网页静态化技术和缓存技术的共同点都是为了减轻数据库的访问压力,但是具体的应用场景不同,缓存比较适合小规模的数据,而网页静态化比较适合大规模且相对变化不太频繁的数据。另外网页静态化还有利于SEO。
package xin.kingsman.filter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import xin.kingsman.customWeb.CustomResponseWrapper;
/**
* Servlet Filter implementation class CustomOutFilter
*/
/**
* @author song
*
*/
@WebFilter("/*")
public class StatichtmlFilter implements Filter {
/**
* Default constructor.
*/
public StaticHtmlFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
* 这里就是一些简单的调用代码
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletResponse httpResp = (HttpServletResponse) response;
CustomResponseWrapper customResponseWrapper = new CustomResponseWrapper(httpResp);
String servletPath = httpReq.getServletPath();
String fileName = servletPath.substring(1)+".html";
chain.doFilter(request, customResponseWrapper);
customResponseWrapper.getWriter().flush();
String str = customResponseWrapper.getContent();
System.out.println(str);
String realPath = httpReq.getServletContext().getRealPath(fileName);
File htmlFile = new File(realPath);
String static_path = htmlFile.getParent()+"\\";
File html = new File(static_path, fileName);
Date htmlDate=null;
Date now = new Date();
if (html.exists()) {
htmlDate = new Date(html.lastModified());
}else {
html.createNewFile();
}
if (htmlDate==null||!sameDate(htmlDate, now)) {
FileOutputStream fileOutputStream = new FileOutputStream(html);
DataOutputStream dataOutputStream=new DataOutputStream(fileOutputStream);
dataOutputStream.writeChars(" ");
dataOutputStream.writeUTF("生成时间"+DateFormat.getDateInstance().format(now));
dataOutputStream.writeUTF(customResponseWrapper.getContent());
dataOutputStream.close();
fileOutputStream.close();
}
System.out.println("/"+fileName);
request.getRequestDispatcher("/"+fileName).forward(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
private boolean sameDate(Date d1, Date d2) {
if(null == d1 || null == d2)
return false;
Calendar cal1 = Calendar.getInstance();
cal1.setTime(d1);
cal1.set(Calendar.HOUR_OF_DAY, 0);
cal1.set(Calendar.MINUTE, 0);
cal1.set(Calendar.SECOND, 0);
cal1.set(Calendar.MILLISECOND, 0);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(d2);
cal2.set(Calendar.HOUR_OF_DAY, 0);
cal2.set(Calendar.MINUTE, 0);
cal2.set(Calendar.SECOND, 0);
cal2.set(Calendar.MILLISECOND, 0);
return cal1.getTime().equals(cal2.getTime());
}
}
嗯,以上就是核心代码。完结撒花
以上是关于死磕基础系列之自定义静态化页面的过滤器的主要内容,如果未能解决你的问题,请参考以下文章
[死磕 Spring 35/43] --- IOC 之自定义类型转换器
[死磕 Spring 35/43] --- IOC 之自定义类型转换器
Flutter之自定义按钮RaisedButtonOutlineButtonIconButton等——Flutter基础系列