在resin服务器上使用gzip页面压缩及利用ant进行空行处理
2011-04-14 11:55
627 查看
web开发中可以通过gzip压缩页面来降低网站的流量,而gzip并不会对cpu造成大量的占用,只是几个百分点而已,但是对于页面却能压缩40%以上,非常划算。
resin服务器具体配置方法:
修改resin/conf/resin.conf文件,在<web-app>中添加以下配置代码:
<filter filter-name="gzip"
filter-class="com.caucho.filters.GzipFilter">
<init>
<use-vary>true</use-vary>
</init>
</filter>
<filter-mapping filter-name="gzip">
<url-pattern>
<exclude-pattern>*.pdf</exclude-pattern>
<include-pattern>/*</include-pattern>
</url-pattern>
</filter-mapping>
此方法只对于专业版本的resin有效。另外可以通过自己写过滤器实现。
非专业版本的resin也可通过ocache来实现。
Resin Open Source
can’t use that filter. You can
write your own, but why should you when there are already good ones out
there? I’ve just the gzip filter in Echache (I usually use Ehcache as
object cache and their filter for filter caching, good stuff!).
Configure your pom to get the web part of Ehcache like this:
And then in your web.xml
, first define the filter:
And then add it in the “right” place in the filter chain (on my site, I deliver Java pages from /blog/
and have all css in /css/
and all javascript in /js/
:
详见http://tech.soulgalore.com/gzip-resin/
自己写的过滤器见如下载包:
http://d.download.csdn.net/source/1929043
另外页面空行删除可以通过ant来实现,如下:
working for you, it may be a more defensive approach to reduce the
filesize of a CSS file with simple regular expression replacement. For
this example, I chose Apache Ant
(using replaceregexp
) to shrink the CSS files.
In this approach, I remove unused whitespace and linebreaks, but do not alter or optimize the CSS code at all.
This can all be done in a few seconds during compile time.
详见http://www.webapp-performance.com/general-tips/removing-unused-css-whitespace-with-ant-replaceregexp
空行删除可以如下:
<replaceregexp match="/r|/n|/t{2,}|/t(?=/r|/n)|(?=/r|/n)/t" replace=""
flags="g" byline="false" encoding="UTF-8">
<fileset dir="${tpl.dir}" includes="**/*.jsp" />
</replaceregexp>
<replaceregexp byline="false" encoding="UTF-8" flags="g">
<regexp pattern="/s+"/>
<substitution expression=" "/>
<fileset dir="${tpl.dir}" includes="**/*.jsp"/>
</replaceregexp>
resin服务器具体配置方法:
修改resin/conf/resin.conf文件,在<web-app>中添加以下配置代码:
<filter filter-name="gzip"
filter-class="com.caucho.filters.GzipFilter">
<init>
<use-vary>true</use-vary>
</init>
</filter>
<filter-mapping filter-name="gzip">
<url-pattern>
<exclude-pattern>*.pdf</exclude-pattern>
<include-pattern>/*</include-pattern>
</url-pattern>
</filter-mapping>
此方法只对于专业版本的resin有效。另外可以通过自己写过滤器实现。
非专业版本的resin也可通过ocache来实现。
Resin Open Source
can’t use that filter. You can
write your own, but why should you when there are already good ones out
there? I’ve just the gzip filter in Echache (I usually use Ehcache as
object cache and their filter for filter caching, good stuff!).
Configure your pom to get the web part of Ehcache like this:
1 2 3 4 5 | <dependency> <groupId> net.sf.ehcache</groupId> <artifactId> ehcache-web</artifactId> <version> 2.0.2</version> </dependency> |
, first define the filter:
1 2 3 4 | <filter> <filter-name> gzipFilter</filter-name> <filter-class> net.sf.ehcache.constructs.web.filter.GzipFilter</filter-class> </filter> |
and have all css in /css/
and all javascript in /js/
:
1 2 3 4 5 | <filter-mapping> <filter-name> gzipFilter</filter-name> <url-pattern> /blog/*</url-pattern> <url-pattern> /css/*.css</url-pattern> <url-pattern> /js/*.js</url-pattern> <dispatcher> REQUEST</dispatcher> <dispatcher> FORWARD</dispatcher> </filter-mapping> |
自己写的过滤器见如下载包:
http://d.download.csdn.net/source/1929043
另外页面空行删除可以通过ant来实现,如下:
Removing unused CSS whitespace with ant replaceregexp
If common CSS compressors are notworking for you, it may be a more defensive approach to reduce the
filesize of a CSS file with simple regular expression replacement. For
this example, I chose Apache Ant
(using replaceregexp
) to shrink the CSS files.
In this approach, I remove unused whitespace and linebreaks, but do not alter or optimize the CSS code at all.
Definition of the affected files
For easy reference, we define a fileset.<fileset id="css.fileset" dir="your-css-folder" includes="**/*.css"/>
Comment removement
Remove comments in one or multiple lines.<replaceregexp match="//*.*/*/" replace="" flags="g" byline="true"> <fileset refid="css.fileset"/> </replaceregexp>
<replaceregexp match="//*.+?/*/" replace="" flags="gs" byline="false"> <fileset refid="css.fileset"/> </replaceregexp>
Whitespace removement
Multiple whitespace characters are reduced to one, leading/ trailing whitespace is removed.<replaceregexp match="/s+" replace=" " flags="g" byline="true"> <fileset refid="css.fileset"/> </replaceregexp> <replaceregexp match="^/s+" replace="" flags="g" byline="true"> <fileset refid="css.fileset"/> </replaceregexp> <replaceregexp match="/s+$" replace="" flags="g" byline="true"> <fileset refid="css.fileset"/> </replaceregexp
Merging of lines
Blocks of CSS statements are collapsed to a single line, multiple line feeds are removed.<replaceregexp match="/{[/n/r]+" replace="{" flags="g" byline="false"> <fileset refid="css.fileset"/> </replaceregexp> <replaceregexp match="[/n/r]+/}" replace="}" flags="g" byline="false"> <fileset refid="css.fileset"/> </replaceregexp> <replaceregexp match="[/n/r]+/{" replace="{" flags="g" byline="false"> <fileset refid="css.fileset"/> </replaceregexp> <replaceregexp match=";[/n/r]+" replace=";" flags="g" byline="false"> <fileset refid="css.fileset"/> </replaceregexp> <replaceregexp match=",[/n/r]+" replace="," flags="g" byline="false"> <fileset refid="css.fileset"/> </replaceregexp> <replaceregexp match="([/n/r])[/n/r]+" replace="/1" flags="g" byline="false"> <fileset refid="css.fileset"/> </replaceregexp>
This can all be done in a few seconds during compile time.
详见http://www.webapp-performance.com/general-tips/removing-unused-css-whitespace-with-ant-replaceregexp
空行删除可以如下:
<replaceregexp match="/r|/n|/t{2,}|/t(?=/r|/n)|(?=/r|/n)/t" replace=""
flags="g" byline="false" encoding="UTF-8">
<fileset dir="${tpl.dir}" includes="**/*.jsp" />
</replaceregexp>
<replaceregexp byline="false" encoding="UTF-8" flags="g">
<regexp pattern="/s+"/>
<substitution expression=" "/>
<fileset dir="${tpl.dir}" includes="**/*.jsp"/>
</replaceregexp>
相关文章推荐
- ASP.NET页面进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)第1/2页
- HttpWebRequest 下载网页时 ,处理页面编码和服务器响应流(Gzip压缩)
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- ASP.NET页面进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源
- ASP.NET页面进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)第1/2页
- ASP.NET页面进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- 在HttpModule中使用gzip,deflate协议对aspx页面进行压缩
- 使用Apache的mod_gzip对WEB服务器进行HTTP压缩优化
- 在HttpModule中使用gzip,deflate协议对aspx页面进行压缩
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(转载)
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试
- 使用ANT对Js/Css文件进行合并和压缩处理 [转]
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试(转载)
- [转]针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- 在HttpModule中使用gzip,deflate协议对aspx页面进行压缩(转)
- 利用Filter对asp.net页面进行压缩