欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用GZIP动态压缩Extjs

程序员文章站 2024-03-13 23:34:58
...

比来做的Ext项目Ext的插件都斗劲大,对于网页的浏览速度也是很是不容乐观的,于是就须要对所加载的js文件进行优化,此中一个办法就是对于较大的js文件可以进行紧缩。紧缩成gzjs格局,然则gzjs后缀名的文件又不克不及被浏览器所熟悉,得须要对办事器端做些处理惩罚。

一、将js格局文件紧缩成gzjs格局。应用gzip.exe打包紧缩后的JS文件,最后生成xx.js.gz,把xx.js.gz文件改成xx.gzjs。紧缩实例: ext-all.js (610KB), gzip.exe紧缩后为ext-all.gzjs(168KB)。
附:gzip应用办法,在号令行下输入: gzip -9 ext-all.js ext-all.js.gz
批改ext-all.js.gz的后缀名ext-all.gzjs(注:也可以经由过程gzip -h号令查看帮助)
地址:http://www.gzip.org

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GzipJsFilter implements Filter {
@SuppressWarnings("unchecked")
Map headers = new HashMap();
public void destroy() { }
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
if (req instanceof HttpServletRequest)
doFilter((HttpServletRequest)req, (HttpServletResponse)res, chain);
else
chain.doFilter(req, res); }
@SuppressWarnings("unchecked")
public void doFilter(HttpServletRequest request, HttpServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
request.setCharacterEncoding("UTF-8");
for (Iterator it = this.headers.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry)it.next();
response.addHeader((String)entry.getKey(), (String)entry.getValue());
}
chain.doFilter(request, response); }
@SuppressWarnings("unchecked")
public void init(FilterConfig config) throws ServletException {
String headersStr = config.getInitParameter("headers");
String[] headers = headersStr.split(",");
for (int i = 0; i < headers.length; ++i) {
String[] temp = headers[i].split("=");
this.headers.put(temp[0].trim(), temp[1].trim());
}
}
}
<filter>
<filter-name>GzipJsFilter</filter-name>
<filter-class>com.byd.core.GzipJsFilter</filter-class>
<init-param>
<param-name>headers</param-name>
<param-value>Content-Encoding=gzip</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>GzipJsFilter</filter-name>
<url-pattern>*.gzjs</url-pattern>
</filter-mapping>