简体   繁体   中英

How to enable the Gzip compression in jBoss EAP server

I am using a jBoss EAP 6.1 sevrer in which I want to Enable the GZIP Compression for my HTML, CSS and javaScript files.

I am entirely new to this compression technology, can anyone suggest me a tutorial or can directly give me a step wise process to enable a compression on Jboss EAP 6.1 server.

Apart from these I have the following doubts:

1) Do I need to compress the files on my workspace or if my jboss server will be compression enabled it will take care of compression , I just need to deploy the war.

2) When server respond my request , Do i need to explicitly convert or decode or decompress the files to use them or to interpret them?

GZIP compression for web resources is optional, so you can't compress all of them and then hope that every web client is able to handle it. That's why it's usually enabled at runtime when the client (might be a web browser) says "gzip is OK for me" with the Accept-Encoding: gzip, deflate header. See https://en.wikipedia.org/wiki/HTTP_compression

On the server side, the magic is handles by a HTTP Filter which intercepts the request, notes the header, then sends the request on to the rest of the app, intercepts the response and compresses accordingly.

JBoss has some built-in support: Enabling gzip compression for Jboss

If you want to do it yourself, you need to write a Filter and configure it in your web.xml .

Java2s has an implementation :

  public void doFilter(ServletRequest req, ServletResponse res,
      FilterChain chain) throws IOException, ServletException {
    if (req instanceof HttpServletRequest) {
      HttpServletRequest request = (HttpServletRequest) req;
      HttpServletResponse response = (HttpServletResponse) res;
      String ae = request.getHeader("accept-encoding");
      if (ae != null && ae.indexOf("gzip") != -1) {        
        GZIPResponseWrapper wrappedResponse = new GZIPResponseWrapper(response);
        chain.doFilter(req, wrappedResponse);
        wrappedResponse.finishResponse();
        return;
      }
      chain.doFilter(req, res);
    }
  }

or you can use a performance optimization library like WebUtilities to enable compression as described here https://github.com/rpatil26/webutilities/wiki/Enable-Compression

See also:

In Jboss EAP 7.0 this worked for me:

edit: Standalone.xml

<subsystem xmlns="urn:jboss:domain:undertow:1.2">   <!-- SEARCH FOR THIS: urn:jboss:domain:undertow -->
  <buffer-cache name="default"/>  
  <server name="default-server">  
  <http-listener name="default" socket-binding="http"/>  
  <host name="default-host" alias="localhost">  
  (...)

  <!-- ADD THIS FOR GZIP COMPRESSION -->
  <filter-ref name="gzipFilter" predicate="exists['%{o,Content-Type}'] and regex[pattern='(?:application/javascript|text/css|text/html|text/xml|application/json)(;.*)?', value=%{o,Content-Type}, full-match=true]"/>  
  <!-- /GZIP COMPRESSION -->

  </host>  
  </server>  
(...)  
  <filters>  
  (...)  

  <!-- ADD THIS FOR GZIP COMPRESSION -->
  <gzip name="gzipFilter"/>  
  <!-- /GZIP COMPRESSION -->

  </filters>  
</subsystem>

Restart the server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM