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.
- Instead of including file types I'd like gziped I include file types I want skipped like jpeg or gif.
- 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.