• 去除响应网页内容中的空白


    当我们打开一个网页之后,查看源代码会发现,很多网页会有空行,如果剔除空行的话,源代码的体积会减少一下。下面是实现方法。

    import java.io.IOException;
    import java.io.PrintWriter;
    
    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.HttpServletResponse;
    
    import com.wappers.HtmlWapper;
    
    
    @WebFilter(filterName="htmlFilter",urlPatterns="/index.jsp")
    public class HtmlFilter  implements Filter {
    
        @Override
        public void destroy() {
            
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("------------------------>进入filter......");
             
            //使用自定义的响应包装器来包装原始的ServletResponse
            HtmlWapper wapper=new HtmlWapper((HttpServletResponse) response);
            chain.doFilter(request, wapper);
            String s=wapper.getResult();
            s=s.replaceAll("(?<=\>)(?:\s*
    ?
    ?)(?=\<)","");
             PrintWriter out=response.getWriter();
            out.write(s);
            out.flush();
        }
    
        @Override
        public void init(FilterConfig arg0) throws ServletException {
            
        }
        
        
    }
    import java.io.CharArrayWriter;
    import java.io.PrintWriter;
    
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletResponseWrapper;
    
    
    public class HtmlWapper extends HttpServletResponseWrapper{
    
        private PrintWriter printWriter;
        
        private CharArrayWriter charWriter;
        
    //    private CompressedStream compressedOut;
        
        public HtmlWapper(HttpServletResponse response) {
            super(response);
            // TODO Auto-generated constructor stub
            charWriter=new CharArrayWriter();
            printWriter=new PrintWriter(charWriter);
            
        }
    
        public String getResult(){
            return charWriter.toString();
            
            
        }
        
        
        @Override
        public PrintWriter getWriter(){
            return printWriter;
        }
        
    }

    效果对比,下面分别是压缩前,压缩后的情况:

    打印网页指定位置:

    var newWindow=window.open("打印窗口","_blank");
    // 假设要打印的区域 包在 div 中,id 为:data_div_id
    var obj = document.getElementById("data_div_id");
    var docStr = obj.innerHTML;
    newWindow.document.write(docStr);
    newWindow.document.close();
    newWindow.print();
  • 相关阅读:
    iOS开发objectc优势与补足
    UITableView详解
    ipad 、iphone开发-通过定时器显示进度条
    [Yii Framework][SHARE] The directory structure of the Yii project site
    [Yii Framework] Yii中事件和行为的区别和应用
    [Ubuntu] 创建桌面启动图标
    [Ubuntu] Access denied for user ‘debiansysmaint’@'localhost’ (using password: YES)
    [Yii Framework] Chive: which is developed base on Yii, and its aims to be an alternative to phpMyAdmin!
    [javascript] 数组扩展操作
    [php] Generate PhpDoc with NetBeans
  • 原文地址:https://www.cnblogs.com/demingblog/p/5193244.html
Copyright © 2020-2023  润新知