filter過濾器的建立package filter;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.util.zip.GZIPOutputStream;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpServletResponseWrapper;public class GzipFilter implements Filter {// 這是一個用來將響應出去的資料進行壓縮的處理過濾的類@Overridepublic void destroy() {// TODO Auto-generated method stub}@Overridepublic void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;System.out.println("doFilter被執行在建立增強器MyRresponse之前.................");// 對response響應的對象進行增強MyRresponse myRresponse=new MyRresponse(response);//執行目標方法帶著增強後的對象chain.doFilter(request, myRresponse);//擷取response對象中的流的原大小byte resouceByte[]=myRresponse.getBuffer();System.out.println("這是原資料的大小==========="+resouceByte.length);//這是壓縮之後的資料對象ByteArrayOutputStream out=new ByteArrayOutputStream();//建立壓縮的流對象,接收位元組輸出資料流GZIPOutputStream gzipOutputStream=new GZIPOutputStream(out);//壓縮流對象,並將資料寫入壓縮輸出資料流對象gzipOutputStream.write(resouceByte);//關閉資料流gzipOutputStream.close();//得到壓縮後的資料byte gzipByte[]=out.toByteArray();System.out.println("這是壓縮後的資料大小==========="+gzipByte.length);//高速瀏覽器以什麼樣的方式進行解析//內容的編碼方式response.setHeader("Content-Encoding", "gzip");//內容大小response.setContentLength(gzipByte.length);//將內容寫入response.getOutputStream().write(gzipByte);}@Overridepublic void init(FilterConfig filterConfig) throws ServletException {// TODO Auto-generated method stub}// 這是對伺服器輸出資料流的增強class MyServletOutputStream extends ServletOutputStream {// 定義位元組的輸出資料流對象private ByteArrayOutputStream baos;public MyServletOutputStream(ByteArrayOutputStream baos) {super();this.baos = baos;}// 重寫輸輸出資料流方法@Overridepublic void write(int b) throws IOException {// TODO Auto-generated method stubbaos.write(b);}}// 這是對響應的資料進行增強類// 1.首先看需要被增強對象繼承了什麼介面或父類,編寫一個類也去繼承這些介面或父類class MyRresponse extends HttpServletResponseWrapper {// 緩衝區輸出資料流的定義private ByteArrayOutputStream baos = new ByteArrayOutputStream();// 2.在類中定義一個變數,變數類型即需要增強對象的類型private HttpServletResponse response;// 建立文本輸出資料流private PrintWriter pw;// 3.在類中定義一個建構函式,接收需增強對象public MyRresponse(HttpServletResponse response) {super(response);this.response = response;}// 4.覆蓋被增強方法,編寫增強功能@Overridepublic ServletOutputStream getOutputStream() throws IOException {return new MyServletOutputStream(baos);}@Overridepublic PrintWriter getWriter() throws IOException {pw = new PrintWriter(new OutputStreamWriter(baos, "UTF-8"));return pw;}// 自訂方法public byte[] getBuffer() {if (pw != null) {// 判斷緩衝區是否寫入資料pw.close();}return baos.toByteArray();}}}
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_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>jqueryeasyui</display-name><servlet><servlet-name>LoginServlet</servlet-name><servlet-class>servlet.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/login.do</url-pattern></servlet-mapping><filter><filter-name>EncodingFilter</filter-name><filter-class>filter.EncodingFilter</filter-class></filter><filter-mapping><filter-name>EncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>GzipFilter</filter-name><filter-class>filter.GzipFilter</filter-class></filter><!-- 過濾指定的尾碼請求--><filter-mapping><filter-name>GzipFilter</filter-name><url-pattern>*.jpg</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>