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:
1 | <dependency> |
And then in your web.xml , first define the filter:
1 | <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/ :
1 | <filter-mapping> |
详见http://tech.soulgalore.com/gzip-resin/
自己写的过滤器见如下载包:
http://d.download.csdn.net/source/1929043
另外页面空行删除可以通过ant来实现,如下:
Removing unused CSS whitespace with ant replaceregexp
If common CSS compressors are not 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.
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>