随着网站的不断扩大,首页面的内容越来越多,甚至要放置赢利性质的广告,而这些内容,每次被用户访问时都是从数据库中读取或者经过业务逻辑处理获得结果,这样会严重影响页面的加载速度,很多网站都是使用静态的html首页面来结局问题。
实现过程
1.创建jsp页面,添加测试内容
2.编写MyOutputStream类他继承自ServletOutputStream类并重写write()方法,使用自己的字节数组输出流实现了数据输出。关键代码如下:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
public class MyOutputStream extends ServletOutputStream {
private ByteArrayOutputStream stream;
public MyOutputStream(ByteArrayOutputStream stream){
this.stream=stream;
}
@Override
public void write(int arg0) throws IOException {
// TODO Auto-generated method stub
stream.write(arg0);
}
}
3.编写ResponseWrapper类,他继承自HttpServletResponseWrapper类,并重写getOutputStream()和getWriter()方法,返回本类定义的输出流,自定义的的getContent()方法可以获取这些输出流的数据。关键代码如下。
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import com.stream.MyOutputStream;
public class ResponseWrapper extends HttpServletResponseWrapper {
private MyOutputStream stream;
private ByteArrayOutputStream byteStream;
private PrintWriter pw;
public ResponseWrapper(HttpServletResponse response) {
super(response);
// TODO Auto-generated constructor stub
this.byteStream=new ByteArrayOutputStream();
this.stream=new MyOutputStream(this.byteStream);
this.pw=new PrintWriter(this.byteStream);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
// TODO Auto-generated method stub
return stream;
}
@Override
public PrintWriter getWriter() throws IOException {
// TODO Auto-generated method stub
return this.pw;
}
public String getContent(){
//取出空格,必须否则在页面中会有其他字符
return this.byteStream.toString().trim();
}
}
4.编写CreateHTMLFilter过滤器,实现Filter接口,在doFilter()方法中实现业务。
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.stream.MyOutputStream;
import com.wrapper.ResponseWrapper;
/**
* Servlet Filter implementation class CreateHTMLFilter
*/
public class CreateHTMLFilter implements Filter {
/**
* Default constructor.
*/
public CreateHTMLFilter() {
// 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 {
// TODO Auto-generated method stub
// place your code here
HttpServletResponse res = (HttpServletResponse) response;
ResponseWrapper wrapper = new ResponseWrapper(res);
// pass the request along the filter chain
chain.doFilter(request, wrapper);
wrapper.getWriter().flush();
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getSession().getServletContext()
.getRealPath("index.jsp");
System.out.println(path);
File file = new File(path);
// 创建要生成的html页面对象
File htmlFile = new File(file.getParentFile(), "index.html");
// html页面创建时间
Date htmlDate = null;
Date now = new Date();
if (!htmlFile.exists()) {
htmlFile.createNewFile();
} else {
htmlDate = new Date(htmlFile.lastModified());
}
// 根据文件的创建时间判断是否需要新建html页面
if (htmlDate == null || htmlDate.getDate() != now.getDate()) {
FileOutputStream fos = new FileOutputStream(htmlFile);
DataOutputStream dos = new DataOutputStream(fos);
System.out.println(wrapper.getContent().trim());
dos.writeUTF(wrapper.getContent());
dos.close();
}
request.getRequestDispatcher("index.html").forward(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
5.web.xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns=http://java.sun.com/xml/ns/javaee xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>dynamic</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<display-name>CreateHTMLFilter</display-name>
<filter-name>CreateHTMLFilter</filter-name>
<filter-class>com.filter.CreateHTMLFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CreateHTMLFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
6. 测试,可以看到你想要的结果了.