筆者將response內回應給使用者的內容,
使用GZIP壓縮的方式回傳給瀏覽器,而IE5和NS6也都有支援Gzip的壓縮格式。
這個方法在之前就有人提出過,因為是讓網頁在輸出時經過壓縮,
可以讓傳輸量變小很多,雖然現在的網路頻寬對於用來看網頁已經綽綽有餘,
但是檔案大小太大的網頁還是會造成一定的影響。
經過Gzip壓縮過的網頁,檔案大小可以到原本壓縮前的20%。
[code]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();
}
}
}[/code]
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壓縮過,
然後瀏覽器將可以直接看壓縮的網頁