The real compression filter code

Time to code. We end this chapter by looking at the code for both the compression filter and the wrapper it uses. We’re expanding from the previous discussion, and while there is some new stuff here, it’s mostly just plain Java code.

This filter provides a mechanism to compress the response body content. This type of filter would commonly be applied to any text content such as HTML, but not to most media formats such as PNG or MPEG, because they are already compressed.

Relax

You don’t need to study this code for the exam.

The rest of this example is a demonstration of a response filter in action, just so that you can see something a little more real-world. You don’t need to learn or understand this particular example for the exam, so consider the rest of this chapter completely optional.

image with no caption

Debugging Tip!

To test this filter, comment out this line of code. You should see illegible, compressed data in your browser.

image with no caption
image with no caption

“Off the path”

Compression meets HTTP

How does the server know it can send compressed data? How does the browser know when it’s getting compressed data? It turns out that HTTP is “compression-aware”; here’s how it works:

  • One of the headers that the browser ...

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.