08-04
16

使用filter机制来GZIP压缩网页

笔者将response内响应给使用者的内容,
使用GZIP压缩的方式回传给浏览器,而IE5和NS6也都有支持Gzip的压缩格式。
这个方法在之前就有人提出过,因为是让网页在输出时经过压缩,
可以让传输量变小很多,虽然现在的网络频宽对于用来看网页已经绰绰有余,
但是档案大小太大的网页还是会造成一定的影响。
经过Gzip压缩过的网页,档案大小可以到原本压缩前的20%。

package com.jsptw.filter;
import java.io.*;
import java.util.zip.GZIPOutputStream;
import javax.servlet.*;
import javax.servlet.http.*;
public class GZIPEncodeFilter implements Filter {
      public void init(FilterConfig filterConfig) {}
      public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain)
                                      throws IOException, ServletException {        
        String transferEncoding = getGZIPEncoding((HttpServletRequest)request);        
        if (transferEncoding == null)
        {            
          chain.doFilter(request, response);        
        }
        else
        {          
          ((HttpServletResponse)response).setHeader("Content-Encoding", transferEncoding);
            GZIPEncodableResponse wrappedResponse = new GZIPEncodableResponse((HttpServletResponse)response);
            chain.doFilter(request, wrappedResponse);          
            wrappedResponse.flush();        
        }    
      }    
      public void destroy() {}    
      private static String getGZIPEncoding(HttpServletRequest request) {        
        String acceptEncoding = request.getHeader("Accept-Encoding");        
          if (acceptEncoding == null)
            return null;        
          acceptEncoding = acceptEncoding.toLowerCase();        
          if (acceptEncoding.indexOf("x-gzip") >= 0)
          {
            return "x-gzip";
          }        
          if (acceptEncoding.indexOf("gzip") >= 0)
          {            
            return "gzip";        
          }        
          return null;    
      }    
      
      private class GZIPEncodableResponse extends HttpServletResponseWrapper {        
        private GZIPServletStream wrappedOut;        
          public GZIPEncodableResponse(HttpServletResponse response) throws IOException {          
            super(response);            
              wrappedOut = new GZIPServletStream(response.getOutputStream());        
          }        
          public ServletOutputStream getOutputStream() throws IOException {            
            return wrappedOut;        
          }        
          private PrintWriter wrappedWriter;        
          public PrintWriter getWriter() throws IOException {            
            if (wrappedWriter == null) {                
                wrappedWriter = new PrintWriter( new OutputStreamWriter( getOutputStream(), getCharacterEncoding()));            }            
                 return wrappedWriter;        
              }        
          public void flush() throws IOException {            
            if (wrappedWriter != null) {              
                wrappedWriter.flush();            
              }          
              wrappedOut.finish();        
          }    
      }    
      
      private class GZIPServletStream extends ServletOutputStream {        
        private GZIPOutputStream outputStream;        
          public GZIPServletStream(OutputStream source) throws IOException {            
         outputStream = new GZIPOutputStream(source);        
       }        
       public void finish() throws IOException {
         OutputStream.finish();        
       }        
      public void write(byte[] buf) throws IOException {            
        outputStream.write(buf);        
      }        
      public void write(byte[] buf, int off, int len) throws IOException {            
        outputStream.write(buf, off, len);        
      }        
      public void write(int c) throws IOException {            
        outputStream.write(c);        
      }        
      public void flush() throws IOException {            
        outputStream.flush();        
      }      
      public void close() throws IOException {            
        outputStream.close();        
      }    
    }
}



web.xml中的设定为
<filter>        
<filter-name>GZIPEncoder</filter-name>        
<filter-class>com.jsptw.filter.GZIPEncodeFilter</filter-class>
</filter>

<filter-mapping>  
<filter-name> GZIPEncoder</filter-name>  
<url-pattern>/*</url-pattern>
</filter-mapping>  



这个范例是笔者要在将来的书中,介绍filter机制时所要用的code
主要是为了介绍filter和filter的wrapper机制
此filter会将设定的网页范围全部用gzip压缩过,
然后浏览器将可以直接看压缩的网页

good luck

koji
--------------------------------------------------------------------------------
2.Re:使用filter机制来GZIP压缩网页 [Re: koji] Copy to clipboard
Posted by: koji
Posted on: 2003-06-15 05:21

如果想知道有没有真的压缩
可以将
if (transferEncoding == null)
{            
  chain.doFilter(request, response);  
}
else
{          
  ((HttpServletResponse)response).setHeader("Content-Encoding", transferEncoding);
  GZIPEncodableResponse wrappedResponse = new GZIPEncodableResponse((HttpServletResponse)response);
  chain.doFilter(request, wrappedResponse);          
  wrappedResponse.flush();        
}



if (transferEncoding == null)
中的动作改成和else中的一样
然后使用
telnet ip port
GET 网址

譬如计算机ip是 127.0.0.1 port不更该使用预设8080
telnet 127.0.0.1 8080
网址放在
http://127.0.0.1:8080/examples/jsp/Hello.html
那就打
GET /examples/jsp/Hello.html

将会看到一堆乱码,也就表示这是压缩过的档案

good luck



文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: GZIP fiter
相关日志:
评论: 0 | 引用: 0 | 查看次数: 663
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭