JavaWeb 过滤器应用之页面静态化

  • 页面静态化是把servlet请求的资源所做输出保存到html中, 然后重定向到 html 页面,
    二次访问时,这个html已经存在,那么直接重定向,不用再去访问servlet!
// StaticFilter
    public class StaticFilter implements Filter{
        private FilterConfig config;

        public void destory(){}
        public void init(FilterConfig fConfig) throws ServletException{
            this.config = fConfig;
        }

        public void doFilter(ServletRequest request, ServletResponse response,
                    FilterChain chain) throws ServletException, IOException{

            /*
             * 思路:
             *    1. 第一次访问时,查找请求对应的html页面是否存在,如果存在,则重定向到html;
             *    2. 如果不存在,放行! 把 servlet 访问数据库后,输出给客户端的数据保存到一个
             *       html文件中, 在重定向到html
             *
             *   获取 Category 参数!
             *   Category有四种可能:
             *    null  对应 null.html
             *    1     对应 1.html
             *    2     对应 2.html
             *    3     对应 3.html
             *
             *    html 页面的保存路径: htmls 目录下
             */

             // 判断对应的 html 文件是否存在, 如果存在,直接重定向!

             HttpServletRequest req = (HttpServletRequest)request;
             HttpServletResponse resp = (HttpServletResponse)response;

             String category = request.getParameter("category");

             // 获取对应的文件名
             String htmlPage = category + ".html";

             //得到文件的存放目录
             String htmlPath = config.getServletContext().getRealPath("/htmls");

             // 创建文件
             File destFile = new File(htmlPath,htmlPage);

             // 如果该文件存在
            if(destFile.exists()){
                response.sendRedirect(request.getContextPath()+"/htmls/"+htmlPage);
                return;
            }

            /*
             * 如果该文件不存在, 生成html
             * 首先放行, show.jsp 会做出很多的输出, 我们要让它不再输出给客户端, 而是输出给我们指定
             * 的一个 html 文件中
             *
             * 调包 response, 让它的 getWriter() 与一个html 文件绑定, 那么 show.jsp 的输出就到了
             * html 文件中
             */

             StaticResponse sr = new StaticResponse(resp,destFile.getAbsolutePath());

            // 放行, 即生成了html 文件
            chain.doFilter(request,sr);

            // 这时,页面已经存在, 重定向到html文件
            res.sendRedirect(req.getContextPath()+"/htmls/"+htmlPage);
        }
    }


    // response 的装饰类, StaticResponse.java
    public class StaticResponse extends HttpServletResponseWrapper {
        private HttpServletResponse response;
        private PrintWriter pw;

        // 参数 path, 表示 html 文件的路径
        public StaticResponse(HttpServletResponse response, String path)
                                throws FileNotFoundException {
            super(response);
            this.response = response;

            // 创建一个与html文件路径绑定在一起的流对象
            pw = new PrintWriter(path, "utf-8");
        }

        // 增强的方法
        public PrintWriter getWriter(){
            // 返回一个与html绑定在一起的 printWriter 对象
            // jsp 会使用它进行输出, 这样数据都输出到 html 文件中
            return pw;
        }
    }

参考资料:

原文地址:https://www.cnblogs.com/linkworld/p/7635783.html