Appfuse2学习笔记--GzipFilter的应用

AppFuse中经过分析使用了大量的开源框架和组件。个人认为整个后台还不是强大,可能与它的定位有关联。我们在项目中积累了大量的Spring以及Hibernate应用都要比之要强很多。但appFuse的前台整合还是相当不错的。先学一个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>  
<filter>
		<filter-name>gzipFilter</filter-name>
		<filter-class>
			net.sf.ehcache.constructs.web.filter.GzipFilter
		</filter-class>
	</filter>
<filter-mapping>
		<filter-name>gzipFilter</filter-name>
		<url-pattern>*.css</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>gzipFilter</filter-name>
		<url-pattern>*.png</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>gzipFilter</filter-name>
		<url-pattern>*.gif</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>gzipFilter</filter-name>
		<url-pattern>*.html</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>gzipFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>gzipFilter</filter-name>
		<url-pattern>*.js</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>gzipFilter</filter-name>
		<url-pattern>*.json</url-pattern>
	</filter-mapping>


效果,你可以用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>  
展开阅读全文

没有更多推荐了,返回首页