O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required