学一个gzipFilter
gzipFilter其实就位于eHcache里面,他是将response中的东东都压缩一下,这个可大大减少了传输时间。
配置web.xml
Java代码
- <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>
-
<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代码
- <%@ page language="java" import="java.util.*,java.net.*,java.io.*"
- pageEncoding="ISO-8859-1"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <%
- String url = request.getParameter("url");
- if (url != null) {
- URL noCompress = new URL(url);
- HttpURLConnection huc = (HttpURLConnection) noCompress
- .openConnection();
- huc.setRequestProperty("user-agent", "Mozilla(MSIE)");
- huc.connect();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- InputStream is = huc.getInputStream();
- while (is.read() != -1) {
- baos.write((byte) is.read());
- }
- byte[] b1 = baos.toByteArray();
- URL compress = new URL(url);
- HttpURLConnection hucCompress = (HttpURLConnection) noCompress
- .openConnection();
- hucCompress.setRequestProperty("accept-encoding", "gzip");
- hucCompress.setRequestProperty("user-agent", "Mozilla(MSIE)");
- hucCompress.connect();
- ByteArrayOutputStream baosCompress = new ByteArrayOutputStream();
- InputStream isCompress = hucCompress.getInputStream();
- while (isCompress.read() != -1) {
- baosCompress.write((byte) isCompress.read());
- }
- byte[] b2 = baosCompress.toByteArray();
- request.setAttribute("t1", new Integer(b1.length));
- request.setAttribute("t2", new Integer(b2.length));
- request.setAttribute("t3", (1 - new Double(b2.length)
- / new Double(b1.length)) * 100);
- }
- request.setAttribute("url", url);
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'MyJsp.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- This is my JSP page.
- <br>
- <h1>
- Compression Test
- </h1>
- Enter a URL to test.
- <form method="POST">
- <input name="url" size="50">
- <input type="submit" value="Check URL">
- </form>
- <p>
- <%=url%>
- <b>Testing: ${url}</b>
- </p>
- Request 1: ${t1} bytes
- <%=request.getAttribute("t1")%>
- <br />
- Request 2: ${t2} bytes
- <%=request.getAttribute("t2")%>
- <br />
- Space saved: ${t1-t2} bytes or ${(1-t2/t1)*100}%
- <%=request.getAttribute("t3")%>%
- <br />
- </body>
- </html>