`
erichua
  • 浏览: 516417 次
  • 性别: Icon_minigender_2
  • 来自: 远方
社区版块
存档分类
最新评论

Appfuse2学习笔记--GzipFilter的应用

阅读更多
AppFuse中经过分析使用了大量的开源框架和组件。个人认为整个后台还不是强大,可能与它的定位有关联。我们在项目中积累了大量的Spring以及Hibernate应用都要比之要强很多。但appFuse的前台整合还是相当不错的。先学一个gzipFilter
gzipFilter其实就位于eHcache里面,他是将response中的东东都压缩一下,这个可大大减少了传输时间。
配置web.xml
<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文件专门评估
<%@ 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>

分享到:
评论
3 楼 erichua 2008-10-10  
我的理解是tomcat等应用服务器是对所有网站内容都进行压缩。而用应用程序可以选择性的进行,更多一些灵活性吧。其实效果是一样的。在应用程序中更灵活一些。

heyosi 写道

erichua 写道AppFuse中经过分析使用了大量的开源框架和组件。个人认为整个后台还不是强大,可能与它的定位有关联。我们在项目中积累了大量的Spring以及Hibernate应用都要比之要强很多。但appFuse的前台整合还是相当不错的。先学一个gzipFilter
gzipFilter其实就位于eHcache里面,他是将response中的东东都压缩一下,这个可大大减少了传输时间。
配置web.xml
Java代码 &lt;filter&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-class&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;net.sf.ehcache.constructs.web.filter.GzipFilter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter-class&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter&gt;&nbsp;&nbsp;&lt;filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;*.css&lt;/url-pattern&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;*.png&lt;/url-pattern&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;*.gif&lt;/url-pattern&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;*.html&lt;/url-pattern&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;*.js&lt;/url-pattern&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;*.json&lt;/url-pattern&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/filter-mapping&gt;&nbsp;&nbsp;&lt;filter&gt;
&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;
&lt;filter-class&gt;
net.sf.ehcache.constructs.web.filter.GzipFilter
&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;*.css&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;*.png&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;*.gif&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;*.js&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;gzipFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;*.json&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;


效果,你可以用FoxFire的net看各个css,js文件可是压缩50%以上哦。
写了一个jsp文件专门评估
Java代码 &lt;%@&nbsp;page&nbsp;language="java"&nbsp;import="java.util.*,java.net.*,java.io.*"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pageEncoding="ISO-8859-1"%&gt;&nbsp;&nbsp;&lt;%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;path&nbsp;=&nbsp;request.getContextPath();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;basePath&nbsp;=&nbsp;request.getScheme()&nbsp;+&nbsp;"://"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;request.getServerName()&nbsp;+&nbsp;":"&nbsp;+&nbsp;request.getServerPort()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;path&nbsp;+&nbsp;"/";&nbsp;&nbsp;%&gt;&nbsp;&nbsp;&lt;%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;url&nbsp;=&nbsp;request.getParameter("url");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(url&nbsp;!=&nbsp;null)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL&nbsp;noCompress&nbsp;=&nbsp;new&nbsp;URL(url);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpURLConnection&nbsp;huc&nbsp;=&nbsp;(HttpURLConnection)&nbsp;noCompress&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.openConnection();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;huc.setRequestProperty("user-agent",&nbsp;"Mozilla(MSIE)");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;huc.connect();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByteArrayOutputStream&nbsp;baos&nbsp;=&nbsp;new&nbsp;ByteArrayOutputStream();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;is&nbsp;=&nbsp;huc.getInputStream();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(is.read()&nbsp;!=&nbsp;-1)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;baos.write((byte)&nbsp;is.read());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte[]&nbsp;b1&nbsp;=&nbsp;baos.toByteArray();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL&nbsp;compress&nbsp;=&nbsp;new&nbsp;URL(url);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpURLConnection&nbsp;hucCompress&nbsp;=&nbsp;(HttpURLConnection)&nbsp;noCompress&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.openConnection();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hucCompress.setRequestProperty("accept-encoding",&nbsp;"gzip");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hucCompress.setRequestProperty("user-agent",&nbsp;"Mozilla(MSIE)");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hucCompress.connect();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByteArrayOutputStream&nbsp;baosCompress&nbsp;=&nbsp;new&nbsp;ByteArrayOutputStream();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;isCompress&nbsp;=&nbsp;hucCompress.getInputStream();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(isCompress.read()&nbsp;!=&nbsp;-1)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;baosCompress.write((byte)&nbsp;isCompress.read());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte[]&nbsp;b2&nbsp;=&nbsp;baosCompress.toByteArray();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.setAttribute("t1",&nbsp;new&nbsp;Integer(b1.length));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.setAttribute("t2",&nbsp;new&nbsp;Integer(b2.length));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.setAttribute("t3",&nbsp;(1&nbsp;-&nbsp;new&nbsp;Double(b2.length)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;new&nbsp;Double(b1.length))&nbsp;*&nbsp;100);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.setAttribute("url",&nbsp;url);&nbsp;&nbsp;%&gt;&nbsp;&nbsp;&lt;!DOCTYPE&nbsp;HTML&nbsp;PUBLIC&nbsp;"-//W3C//DTD&nbsp;HTML&nbsp;4.01&nbsp;Transitional//EN"&gt;&nbsp;&nbsp;&lt;html&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;head&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;base&nbsp;href="&lt;%=basePath%&gt;"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My&nbsp;JSP&nbsp;'MyJsp.jsp'&nbsp;starting&nbsp;page&lt;/title&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;http-equiv="pragma"&nbsp;content="no-cache"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;http-equiv="cache-control"&nbsp;content="no-cache"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;http-equiv="expires"&nbsp;content="0"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;http-equiv="keywords"&nbsp;content="keyword1,keyword2,keyword3"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;http-equiv="description"&nbsp;content="This&nbsp;is&nbsp;my&nbsp;page"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;link&nbsp;rel="stylesheet"&nbsp;type="text/css"&nbsp;href="styles.css"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/head&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;is&nbsp;my&nbsp;JSP&nbsp;page.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Compression&nbsp;Test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/h1&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enter&nbsp;a&nbsp;URL&nbsp;to&nbsp;test.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;form&nbsp;method="POST"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;name="url"&nbsp;size="50"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="submit"&nbsp;value="Check&nbsp;URL"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=url%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;b&gt;Testing:&nbsp;${url}&lt;/b&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/p&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Request&nbsp;1:&nbsp;${t1}&nbsp;bytes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=request.getAttribute("t1")%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&nbsp;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Request&nbsp;2:&nbsp;${t2}&nbsp;bytes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=request.getAttribute("t2")%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&nbsp;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Space&nbsp;saved:&nbsp;${t1-t2}&nbsp;bytes&nbsp;or&nbsp;${(1-t2/t1)*100}%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%=request.getAttribute("t3")%&gt;%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&nbsp;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/body&gt;&nbsp;&nbsp;&lt;/html&gt;&nbsp;&nbsp;&lt;%@ page language="java" import="java.util.*,java.net.*,java.io.*"
pageEncoding="ISO-8859-1"%&gt;
&lt;%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%&gt;
&lt;%
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);
%&gt;
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;base href="&lt;%=basePath%&gt;"&gt;

&lt;title&gt;My JSP 'MyJsp.jsp' starting page&lt;/title&gt;

&lt;meta http-equiv="pragma" content="no-cache"&gt;
&lt;meta http-equiv="cache-control" content="no-cache"&gt;
&lt;meta http-equiv="expires" content="0"&gt;
&lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;
&lt;meta http-equiv="description" content="This is my page"&gt;
&lt;!--
&lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;
--&gt;

&lt;/head&gt;

&lt;body&gt;
This is my JSP page.
&lt;br&gt;
&lt;h1&gt;
Compression Test
&lt;/h1&gt;
Enter a URL to test.
&lt;form method="POST"&gt;
&lt;input name="url" size="50"&gt;
&lt;input type="submit" value="Check URL"&gt;
&lt;/form&gt;
&lt;p&gt;
&lt;%=url%&gt;
&lt;b&gt;Testing: ${url}&lt;/b&gt;
&lt;/p&gt;
Request 1: ${t1} bytes
&lt;%=request.getAttribute("t1")%&gt;
&lt;br /&gt;
Request 2: ${t2} bytes
&lt;%=request.getAttribute("t2")%&gt;
&lt;br /&gt;
Space saved: ${t1-t2} bytes or ${(1-t2/t1)*100}%
&lt;%=request.getAttribute("t3")%&gt;%
&lt;br /&gt;
&lt;/body&gt;
&lt;/html&gt;

应用服务器应该都支持压缩吧,weblogic和tomcat都可以,在app中实现有什么好处呢?不会增加复杂性和出错的概率吗?

2 楼 erichua 2008-10-10  
我的理解是tomcat等应用服务器是对所有网站内容都进行压缩。而用应用程序可以选择性的进行,更多一些灵活性吧。其实效果是一样的。在应用程序中更灵活一些。
1 楼 heyosi 2008-10-07  
erichua 写道
AppFuse中经过分析使用了大量的开源框架和组件。个人认为整个后台还不是强大,可能与它的定位有关联。我们在项目中积累了大量的Spring以及Hibernate应用都要比之要强很多。但appFuse的前台整合还是相当不错的。先学一个gzipFilter
gzipFilter其实就位于eHcache里面,他是将response中的东东都压缩一下,这个可大大减少了传输时间。
配置web.xml
<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文件专门评估
<%@ 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>


应用服务器应该都支持压缩吧,weblogic和tomcat都可以,在app中实现有什么好处呢?不会增加复杂性和出错的概率吗?

相关推荐

    建立项目原型骨架的步骤(最新版本appfuse)appfuse2.1.0-M2

    `mvn archetype:generate -B -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-struts-archetype -DarchetypeVersion=2.1.0-M2 -DgroupId=cn.xue.app -DartifactId=xueAppFuse` ...

    appfuse 学习笔记

    ### Appfuse 学习笔记 #### 一、Appfuse 简介 Appfuse 是一个开源框架,旨在帮助开发者高效地构建企业级应用。通过提供一套完善的架构模板、最佳实践和技术栈组合,使得开发者能够专注于业务逻辑的实现,而不是...

    appfuse-light-webwork-spring-jdbc-1.8.2.zip_Java 8_appfuse_webwo

    在"appfuse-light-webwork-spring-jdbc-1.8.2.zip"这个压缩包中,我们能看到一个基于Java 8、WebWork、Spring和JDBC的项目实例。 **Java 8** Java 8是Java平台的重大更新,引入了许多新特性,如lambda表达式、函数...

    appfuse-tutorial-struts-1.6.zip_appfuse

    这个"appfuse-tutorial-struts-1.6.zip"文件是一个基于Struts 1.6的AppFuse教程,用于指导开发者如何构建一个企业级的人员管理系统。Struts是Apache软件基金会下的一个开源框架,专门用于构建基于MVC(Model-View-...

    appfuse学习笔记(一)安装部署

    替换 `&lt;选择的模板&gt;` 为下载的模板名称,如 `appfuse-basic`,`appfuse-minimal` 等,`&lt;对应版本&gt;` 是你下载的 AppFuse 版本号。 **4. 编译与运行** 进入新创建的项目目录,使用 Maven 编译并运行项目: ``` cd my...

    appfuse2学习日记

    ### AppFuse2 学习知识点总结 #### 一、AppFuse 概述 - **定义与价值**:AppFuse 是一款开源项目,旨在利用一系列开源工具帮助开发者高效地搭建 Web 应用程序的基础架构。通过使用 AppFuse,开发人员可以在构建新...

    AppFuse学习笔记(J2EE入门级框架)

    【AppFuse 框架详解】 AppFuse 是一个由 Matt Raible 创建的开源项目,它为初学者提供了... mvn archetype:create -DarchetypeGroupId=org.appfuse -DarchetypeArtifactId=appfuse-basic-spring -DremoteRepositories=...

    appfuse-documentation-2.1.0官方文档

    综上所述,AppFuse 不仅是一个强大的开发框架,还是一个全面的学习资源库,可以帮助开发者快速掌握现代 Web 开发所需的各种技能。无论你是初学者还是有经验的开发者,都能从这份文档中获益良多。

    appfuse学习笔记(二)新建模块

    在本篇“appfuse学习笔记(二)新建模块”中,我们将深入探讨AppFuse框架的模块创建过程。AppFuse是一个开源项目,它提供了一个快速开发Web应用的基础结构,旨在简化开发流程并提高代码质量。通过AppFuse,开发者...

    appfuse-service-3.0.0.zip

    AppFuse Service 3.0.0 是一个开源项目,它提供了一个基础框架,用于快速开发企业级Java应用。这个版本的亮点在于其集成了一系列现代开发工具和最佳实践,旨在提高开发效率和代码质量。AppFuse 的核心理念是简化复杂...

    Appfuse 2.doc

    如果网络速度较慢,可以选择手动下载并解压`appfuse-2.0-rc1-dependencies.zip`到该目录。 #### 六、不同配置类型的Maven命令示例 除了Struts 2 Basic之外,Appfuse 2.0还支持其他几种配置类型,每种配置类型的...

    appfuse学习笔记(三)解决乱码和菜单设置

    在本篇“appfuse学习笔记(三)解决乱码和菜单设置”中,我们将深入探讨在使用AppFuse框架时遇到的编码问题以及如何定制应用程序的菜单。AppFuse是一款开源项目,它提供了一个快速开发Web应用的基础,特别是对于Java...

    玩转appfuse--使用appfuse建设MVC网站

    2. **项目初始化**:运行AppFuse的生成器,选择合适的项目类型(如基本的MVC应用),指定数据库和编程语言(如Java或Groovy)。 3. **代码生成**:AppFuse会自动生成基本的项目结构,包括模型、控制器、视图和...

    Appfuse2搭建文档

    mvn archetype:generate -DarchetypeGroupId=org.appfuse -DarchetypeArtifactId=appfuse-basic -DarchetypeVersion=2.x -DgroupId=com.mycompany -DartifactId=myproject -Ddb=oracle ``` 这将根据你的配置生成一个...

    AppFuse Primer

    - 书中提供了大量的示例代码和实用指南,旨在帮助读者深入了解AppFuse的工作原理并学习如何使用它来构建高质量的Web应用程序。 #### 三、AppFuse Primer的主要内容概述 - **基础知识**: 在开始使用AppFuse之前,...

    Using Struts 2 - AppFuse 2 - Confluence(1).pdf

    本教程的所有代码都位于Google Code上的`appfuse-demos`项目中的`tutorial-struts2`模块。你可以使用以下的Subversion命令来检出该项目: ``` svn checkout ...

Global site tag (gtag.js) - Google Analytics