encahce之GzipFilter的应用

学一个gzipFilter 
gzipFilter其实就位于eHcache里面,他是将response中的东东都压缩一下,这个可大大减少了传输时间。 
配置web.xml 

Java代码 

  1. <filter>   
  2.         <filter-name>gzipFilter</filter-name>   
  3.         <filter-class>   
  4.             net.sf.ehcache.constructs.web.filter.GzipFilter   
  5.         </filter-class>   
  6.     </filter>   
  7. <filter-mapping>   
  8.         <filter-name>gzipFilter</filter-name>   
  9.         <url-pattern>*.css</url-pattern>   
  10.     </filter-mapping>   
  11.     <filter-mapping>   
  12.         <filter-name>gzipFilter</filter-name>   
  13.         <url-pattern>*.png</url-pattern>   
  14.     </filter-mapping>   
  15.     <filter-mapping>   
  16.         <filter-name>gzipFilter</filter-name>   
  17.         <url-pattern>*.gif</url-pattern>   
  18.     </filter-mapping>   
  19.     <filter-mapping>   
  20.         <filter-name>gzipFilter</filter-name>   
  21.         <url-pattern>*.html</url-pattern>   
  22.     </filter-mapping>   
  23.     <filter-mapping>   
  24.         <filter-name>gzipFilter</filter-name>   
  25.         <url-pattern>*.jsp</url-pattern>   
  26.     </filter-mapping>   
  27.     <filter-mapping>   
  28.         <filter-name>gzipFilter</filter-name>   
  29.         <url-pattern>*.js</url-pattern>   
  30.     </filter-mapping>   
  31.     <filter-mapping>   
  32.         <filter-name>gzipFilter</filter-name>   
  33.         <url-pattern>*.json</url-pattern>   
  34.     </filter-mapping>  
 
  1. <filter>

  2. <filter-name>gzipFilter</filter-name>

  3. <filter-class>

  4. net.sf.ehcache.constructs.web.filter.GzipFilter

  5. </filter-class>

  6. </filter>

  7. <filter-mapping>

  8. <filter-name>gzipFilter</filter-name>

  9. <url-pattern>*.css</url-pattern>

  10. </filter-mapping>

  11. <filter-mapping>

  12. <filter-name>gzipFilter</filter-name>

  13. <url-pattern>*.png</url-pattern>

  14. </filter-mapping>

  15. <filter-mapping>

  16. <filter-name>gzipFilter</filter-name>

  17. <url-pattern>*.gif</url-pattern>

  18. </filter-mapping>

  19. <filter-mapping>

  20. <filter-name>gzipFilter</filter-name>

  21. <url-pattern>*.html</url-pattern>

  22. </filter-mapping>

  23. <filter-mapping>

  24. <filter-name>gzipFilter</filter-name>

  25. <url-pattern>*.jsp</url-pattern>

  26. </filter-mapping>

  27. <filter-mapping>

  28. <filter-name>gzipFilter</filter-name>

  29. <url-pattern>*.js</url-pattern>

  30. </filter-mapping>

  31. <filter-mapping>

  32. <filter-name>gzipFilter</filter-name>

  33. <url-pattern>*.json</url-pattern>

  34. </filter-mapping>

  35.  



效果,你可以用FoxFire的net看各个css,js文件可是压缩50%以上哦。 
写了一个jsp文件专门评估 

Java代码  

  1. <%@ page language="java" import="java.util.*,java.net.*,java.io.*"  
  2.     pageEncoding="ISO-8859-1"%>   
  3. <%   
  4.     String path = request.getContextPath();   
  5.     String basePath = request.getScheme() + "://"  
  6.             + request.getServerName() + ":" + request.getServerPort()   
  7.             + path + "/";   
  8. %>   
  9. <%   
  10.     String url = request.getParameter("url");   
  11.     if (url != null) {   
  12.         URL noCompress = new URL(url);   
  13.         HttpURLConnection huc = (HttpURLConnection) noCompress   
  14.                 .openConnection();   
  15.         huc.setRequestProperty("user-agent", "Mozilla(MSIE)");   
  16.         huc.connect();   
  17.         ByteArrayOutputStream baos = new ByteArrayOutputStream();   
  18.         InputStream is = huc.getInputStream();   
  19.         while (is.read() != -1) {   
  20.             baos.write((byte) is.read());   
  21.         }   
  22.         byte[] b1 = baos.toByteArray();   
  23.   
  24.         URL compress = new URL(url);   
  25.         HttpURLConnection hucCompress = (HttpURLConnection) noCompress   
  26.                 .openConnection();   
  27.         hucCompress.setRequestProperty("accept-encoding", "gzip");   
  28.         hucCompress.setRequestProperty("user-agent", "Mozilla(MSIE)");   
  29.         hucCompress.connect();   
  30.         ByteArrayOutputStream baosCompress = new ByteArrayOutputStream();   
  31.         InputStream isCompress = hucCompress.getInputStream();   
  32.         while (isCompress.read() != -1) {   
  33.             baosCompress.write((byte) isCompress.read());   
  34.         }   
  35.         byte[] b2 = baosCompress.toByteArray();   
  36.         request.setAttribute("t1", new Integer(b1.length));   
  37.         request.setAttribute("t2", new Integer(b2.length));   
  38.         request.setAttribute("t3", (1 - new Double(b2.length)   
  39.                 / new Double(b1.length)) * 100);   
  40.     }   
  41.     request.setAttribute("url", url);   
  42. %>   
  43. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  44. <html>   
  45.     <head>   
  46.         <base href="<%=basePath%>">   
  47.   
  48.         <title>My JSP 'MyJsp.jsp' starting page</title>   
  49.   
  50.         <meta http-equiv="pragma" content="no-cache">   
  51.         <meta http-equiv="cache-control" content="no-cache">   
  52.         <meta http-equiv="expires" content="0">   
  53.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  54.         <meta http-equiv="description" content="This is my page">   
  55.         <!--   
  56.     <link rel="stylesheet" type="text/css" href="styles.css">   
  57.     -->   
  58.   
  59.     </head>   
  60.   
  61.     <body>   
  62.         This is my JSP page.   
  63.         <br>   
  64.         <h1>   
  65.             Compression Test   
  66.         </h1>   
  67.         Enter a URL to test.   
  68.         <form method="POST">   
  69.             <input name="url" size="50">   
  70.             <input type="submit" value="Check URL">   
  71.         </form>   
  72.         <p>   
  73.             <%=url%>   
  74.             <b>Testing: ${url}</b>   
  75.         </p>   
  76.         Request 1: ${t1} bytes   
  77.         <%=request.getAttribute("t1")%>   
  78.         <br />   
  79.         Request 2: ${t2} bytes   
  80.         <%=request.getAttribute("t2")%>   
  81.         <br />   
  82.         Space saved: ${t1-t2} bytes or ${(1-t2/t1)*100}%   
  83.         <%=request.getAttribute("t3")%>%   
  84.         <br />   
  85.     </body>   
  86. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值