1

I have just finished writing a filter to gzip my responses with amazing results. My largest JSP page has gone from 80K to 5.7K. However, the filter I've written gzip's all responses from the server including images. However, Yahoo says this is a very bad practice.

Image and PDF files should not be gzipped because they are already compressed. 
Trying to gzip them not only wastes CPU but can potentially increase file sizes. 

Here is my web.xml footprint of the filter I've written

<!-- THIS FILTER WILL TAKE MY OUTPUT AND GZIP IT TO LESSEN MY BANDWIDTH FOOTPRINT -->
<filter>
    <filter-name>GZip</filter-name>
    <filter-class>org.instride.gzip.GZIPFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>GZip</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

So as you can see everything that is a response get's gzipped. However, I only want to gzip css js and jsp pages. So you'd think I could just add these guys

<url-pattern>*.jsp</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.js</url-pattern>

But that would give me an issue b/c all of my jsp pages hide extensions b/c they are servlets, and I also use tuckey's URL rewrite. So for example the URL of a page I'd want gzipped looks like this.

https://example.com/user/1/admin

So the servlet (user) doesen't have an extension to pinpoint in the web.xml file, and the servlet is followed by forward slash parameters. So I'm thinking of two possible solutions.

  1. Instead of including file types I'd like gziped I include file types I want skipped like jpeg or gif.
  2. I specify to only gzip files based on their content type like text/html;charset=UTF-8 for example.

Any guidance on how I could do either of those things, or a much better solution to my problem would be greatly appreciated. Thank you all for reading this.

4

1 回答 1

0

I went off of the idea I had from number two and wrote this solution. In my solution I look at the accept attribute within the request header. From what I've gathered looking at firebug all of the images on my site no matter what their format have an accept attribute of

image/png,image/*;q=0.8,*/*;q=0.5

I honestly have no idea what that means exactly but all jpg's png's and gif's have it and no other file types do so this is what I did.

...

public class GZIPFilter implements Filter 
{
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException 
    {
        if (servletRequest instanceof HttpServletRequest) 
        {
            HttpServletRequest httpRequest;
            HttpServletResponse httpResponse;
            String acceptedEncoding, accept;
            GZIPResponseWrapper wrappedResponse;

            httpRequest = (HttpServletRequest) servletRequest;
            httpResponse = (HttpServletResponse) servletResponse;

            accept = httpRequest.getHeader("accept");
            acceptedEncoding = httpRequest.getHeader("accept-encoding");

            if (acceptedEncoding != null && acceptedEncoding.indexOf("gzip") != -1 && !accept.equals("image/png,image/*;q=0.8,*/*;q=0.5")) 
            {
                wrappedResponse = new GZIPResponseWrapper(httpResponse);
                filterChain.doFilter(servletRequest, wrappedResponse);
                wrappedResponse.finishResponse();
                return;
            }

            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    public void init(FilterConfig filterConfig) 
    {

    }

    public void destroy() 
    {

    }
}
于 2013-03-30T21:01:16.183 回答