Appfuse2学习笔记--GzipFilter的应用
AppFuse中经过分析使用了大量的开源框架和组件。个人认为整个后台还不是强大,可能与它的定位有关联。我们在项目中积累了大量的Spring以及Hibernate应用都要比之要强很多。但appFuse的前台整合还是相当不错的。先学一个gzipFilter
gzipFilter其实就位于eHcache里面,他是将response中的东东都压缩一下,这个可大大减少了传输时间。
配置web.xml
应用服务器应该都支持压缩吧,weblogic和tomcat都可以,在app中实现有什么好处呢?不会增加复杂性和出错的概率吗?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>
应用服务器应该都支持压缩吧,weblogic和tomcat都可以,在app中实现有什么好处呢?不会增加复杂性和出错的概率吗?