在实际当中大量的js和页面内容使得页面呈现要花很长时间,特别是使用ext。如是找到这种办法去处理一下,还没有测试有多大的优化性能。全部实现代码在附件中
public class CompressionFilter implements Filter { // 适合在上JBoss, Jetty, JOnAS, OC4J, Orion,and Tomcat,其它的可能有bug static boolean useFilter = true; public static final String ENCODING = "UTF-8"; private static final String _GZIP = "gzip"; private static final String _ALREADY_FILTERED = CompressionFilter.class.getName() + "_ALREADY_FILTERED"; private static final String _COMPRESS = "compress"; public static String[] BOOLEANS = {"true", "t", "y", "on", "1"}; private static Log _log = LogFactory.getLog(CompressionFilter.class); public void init(FilterConfig config) { } public void doFilter( ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { if (_log.isDebugEnabled()) { if (useFilter) { _log.debug("Compression is enabled"); } else { _log.debug("Compression is disabled"); } } HttpServletRequest httpReq = (HttpServletRequest)req; HttpServletResponse httpRes = (HttpServletResponse)res; String completeURL = httpReq.getRequestURL().toString(); if (useFilter && isCompress(httpReq) && !isInclude(httpReq) && acceptsGzip(httpReq) && !isAlreadyFiltered(httpReq)) { if (_log.isDebugEnabled()) { _log.debug("Compressing " + completeURL); } httpReq.setAttribute(_ALREADY_FILTERED, Boolean.TRUE); CompressionResponse compressionResponse = new CompressionResponse(httpRes); chain.doFilter(req, compressionResponse); compressionResponse.finishResponse(); } else { if (_log.isDebugEnabled()) { _log.debug("Not compressing " + completeURL); } chain.doFilter(req, res); } } public void destroy() { } protected boolean isAlreadyFiltered(HttpServletRequest req) { if (req.getAttribute(_ALREADY_FILTERED) != null) { return true; } else { return false; } } protected boolean isCompress(HttpServletRequest req) { if (!getBoolean(req.getParameter(_COMPRESS), true)) { return false; } else { return true; } } protected boolean isInclude(HttpServletRequest req) { String uri = (String)req.getAttribute("javax.servlet.include.request_uri"); if (uri == null) { return false; } else { return true; } } protected boolean acceptsGzip(HttpServletRequest req) { String acceptEncoding = req.getHeader("Accept-Encoding"); if ((acceptEncoding != null) && (acceptEncoding.indexOf(_GZIP) != -1)) { return true; } else { return false; } } protected boolean getBoolean(String value, boolean defaultValue) { if (value != null) { try { value = value.trim(); if (value.equalsIgnoreCase(BOOLEANS[0]) || value.equalsIgnoreCase(BOOLEANS[1]) || value.equalsIgnoreCase(BOOLEANS[2]) || value.equalsIgnoreCase(BOOLEANS[3]) || value.equalsIgnoreCase(BOOLEANS[4])) { return true; } else { return false; } } catch (Exception e) { } } return defaultValue; } }
1 楼
sadfasfasd
2008-03-02
兄弟,你的头像是哪部电影? 我想看看
2 楼
jasongreen
2008-03-05
我也想看看
哥们,你的blog因为你的头像肯定流量大增啊
美女 + 美女 = 太美了
美女 kiss 美女,要命啊
哥们,你的blog因为你的头像肯定流量大增啊
美女 + 美女 = 太美了
美女 kiss 美女,要命啊