什么是GZIP JSF-Seam Web应用程序页面的最佳方法

我正在开发一个关于Tomcat的JSF Web应用程序,计划在不久的将来使用Seam,我想添加我们的网页和资源(即Javascript和CSS文件)的压缩。 我知道Java Web中GZIP响应的三种方法:

  1. 使用Ehcache GZIPfilter:它在Appfuse中使用,所以它可能是可靠的,它会在应用之前检查用户代理是否支持GZIP,但它似乎与Seam有问题,我们将使用http://seamframework.org/Community / EHCacheGZipFilterIncompatibleWithSeam 。

  2. 使用pjl-filter。 从stackoverflow问题: Tomcat压缩不添加内容编码:在标头中的gzip ,它似乎没有任何内存泄漏,但我不知道它是否有Seam问题。

  3. 使用Tomcat的内置压缩 – 尽管它可能不提供内容编码(Tomcat 6.0.14似乎工作正常,但您只能为不应用的用户代理压缩提供黑名单。

有没有人在JSF-Seam环境中有这些方法的经验? 哪个是“最佳”解决方案?

谢谢,格伦

GZIPfilter将显着减少初始加载时间。
您还可以实现cacheFilter,以使您的屏幕性能与基于JavaScript的UI( https://stackoverflow.com/a/35567540/5076414 )相同。
对于客户端组件,您可以使用基于JQuery的UI的Primefaces。

在JSF中启用GZIPfilter

只需将此添加到您的

web.xml中

 gzipResponseFilter org.omnifaces.filter.GzipResponseFilter  The threshold size in bytes. Must be a number between 0 and 9999. Defaults to 150. threshold 150   The mimetypes which needs to be compressed. Must be a commaseparated string. Defaults to the below values. mimetypes  text/plain, text/html, text/xml, text/css, text/javascript, text/csv, text/rtf, application/xml, application/xhtml+xml, application/x-javascript, application/javascript, application/json, image/svg+xml, image/gif, application/x-font-woff, application/font-woff2, image/png     gzipResponseFilter /* REQUEST ERROR   java.lang.Throwable /  

以下是你的

的pom.xml

   org.omnifaces omnifaces 1.11  

如何validation我的屏幕是否使用gzip

要查看您的内容是否已经使用gzip和缓存,请在您的Google Chrome浏览器中 – >右键单击您的屏幕 – >检查 – >单击网络选项卡 – >刷新屏幕。 单击图像,图标,样式表,看看您是否在响应标题中看到以下内容

Content-Encoding:gzip如果element的状态是200,则Content-Encoding:gzip

如何添加nginx前端并让它进行压缩(和缓存)?

http://wiki.nginx.org/Main

在这种情况下,属于serverfalut 🙂

你应该尝试Jawr API

我们在JBoss AS代理上使用JBoss Seam ,并使用mod_deflateapache + mod_proxy_ajp后面加载平衡以压缩传出流量。

此设置的优点

  • 网上有很多例子
  • 考虑到不同用户代理的怪癖,易于配置/调试
  • 在运行时更改配置( apachectl graceful而不是重新启动webapp / tomcat以反映更改)。

使用Tomcat的内置压缩 – 尽管它可能不提供内容编码(Tomcat 6.0.14似乎工作正常,但您只能为不应用的用户代理压缩提供黑名单。

我认为你误解了你在Tomcat压缩中发现的问题不会添加内容编码:在标题中的gzip 。 这个问题是由于在Tomcat之前使用带有mod_jk的Apache HTTPD引起的,而后者又被错误地配置为不从Tomcat发回Content-Encoding头。 此问题不是由Tomcat本身引起的。 Tomcat完美地完成了它的工作。

我会说,继续使用Tomcat的内置压缩。 它就像在server.xml的HTTP连接器中添加compression="on"属性一样简单。 您还可以在noCompressionUserAgents设置旁边使用compressableMimeType设置。 阅读HTTP连接器文档 。

我尝试了一个Servletfilter来添加GZIP压缩(虽然不是Ehcache)并且无法使其正常工作。 我最终将Apache与mod_jk放在应用服务器的前面。 所有这些只需要几分钟来配置GIP压缩,我也感觉更安全,因为只有一个应用程序被暴露而不是整个App Server。

另一种Servletfilter可以在这里找到:

http://onjava.com/pub/a/onjava/2003/11/19/filters.html

像Ehcache一样,它会测试浏览器是否支持它。 我不能断然说它是否能与Seam很好地配合使用,但我过去使用它并没有遇到麻烦。

在经过一些黑客攻击后,我对EhCachefilter感到满意。 下面是它的工作原理:

 package myapp; import net.sf.ehcache.constructs.web.GenericResponseWrapper; import net.sf.ehcache.constructs.web.ResponseUtil; import static org.jboss.seam.ScopeType.STATELESS; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.annotations.intercept.BypassInterceptors; import org.jboss.seam.annotations.web.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.GZIPOutputStream; /** * Zip content before sending to the browser. * * */ @Name("gzipFilter") @Scope(STATELESS) @BypassInterceptors @Filter(around = "org.jboss.seam.web.ajax4jsfFilterInstantiator") public class GzipFilter extends net.sf.ehcache.constructs.web.filter.Filter { private static final Logger LOG = Logger.getLogger(GzipFilter.class.getName()); /** * Performs initialisation. * * @param filterConfig config */ protected void doInit(FilterConfig filterConfig) throws Exception { //nothing required. } /** * A template method that performs any Filter specific destruction tasks. * Called from {@link #destroy()} */ protected void doDestroy() { //noop } /** * Performs the filtering for a request. */ protected void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws Exception { if (!isDocStore(request) && !isIncluded(request) && acceptsEncoding(request, "gzip")) { // Client accepts zipped content if (LOG.isLoggable(Level.FINE)) { LOG.fine(request.getRequestURL() + ". Writing with gzip compression"); } // Create a gzip stream final ByteArrayOutputStream compressed = new ByteArrayOutputStream(); final GZIPOutputStream gzout = new GZIPOutputStream(compressed); // Handle the request final GenericResponseWrapper wrapper = new GenericResponseWrapper(response, gzout); chain.doFilter(request, wrapper); wrapper.flush(); gzout.close(); //return on error or redirect code, because response is already committed int statusCode = wrapper.getStatus(); if (statusCode != HttpServletResponse.SC_OK) { return; } //Saneness checks byte[] compressedBytes = compressed.toByteArray(); boolean shouldGzippedBodyBeZero = ResponseUtil.shouldGzippedBodyBeZero(compressedBytes, request); boolean shouldBodyBeZero = ResponseUtil.shouldBodyBeZero(request, wrapper.getStatus()); if (shouldGzippedBodyBeZero || shouldBodyBeZero) { compressedBytes = new byte[0]; } // Write the zipped body //ResponseUtil.addGzipHeader(response); response.setHeader("Content-Encoding", "gzip"); response.setContentLength(compressedBytes.length); response.getOutputStream().write(compressedBytes); } else { // Client does not accept zipped content - don't bother zipping if (LOG.isLoggable(Level.FINE)) { LOG.fine(request.getRequestURL() + ". Writing without gzip compression because the request does not accept gzip."); } chain.doFilter(request, response); } } /** * Checks if the request uri is an include. * These cannot be gzipped. * * @param request the request * @return true if included */ private boolean isIncluded(final HttpServletRequest request) { final String uri = (String) request.getAttribute("javax.servlet.include.request_uri"); final boolean includeRequest = !(uri == null); if (includeRequest && LOG.isLoggable(Level.FINE)) { LOG.fine(request.getRequestURL() + " resulted in an include request. This is unusable, because" + "the response will be assembled into the overrall response. Not gzipping."); } return includeRequest; } private boolean isDocStore(final HttpServletRequest request) { return request.getRequestURI().indexOf("/docstore/") > 0; } /** * Determine whether the user agent accepts GZIP encoding. This feature is part of HTTP1.1. * If a browser accepts GZIP encoding it will advertise this by including in its HTTP header: * 

*
* Accept-Encoding: gzip * *

* Requests which do not accept GZIP encoding fall into the following categories: *
    *
  • Old browsers, notably IE 5 on Macintosh. *
  • Internet Explorer through a proxy. By default HTTP1.1 is enabled but disabled when going * through a proxy. 90% of non gzip requests seen on the Internet are caused by this. *
* As of September 2004, about 34% of Internet requests do not accept GZIP encoding. * * @param request the request * @return true, if the User Agent request accepts GZIP encoding */ protected boolean acceptsGzipEncoding(HttpServletRequest request) { return acceptsEncoding(request, "gzip"); } }