Handy Servlet filter

One useful feature of the later Java Servlet specifications is support for filters. Essentially, a filter can be defined declaratively in a J2EE web application descriptor to intercept responses from the application to the client according to URL patterns. The filter can then do some work on the response, before allowing it to continue.

A really useful example of this, which I have just started using in just about all of my J2EE webapps, is the trim filter. This filter will trim all the extraneous whitespace which gets included in resoponses from JSPs. Just declare the filter with a mapping containing a URL pattern of *.jsp and the filter will take care of this annoying problem. This can reduce the size of the response to the client considerably - I estimate by as much as 40% in one of my applications.

This was previously published at http://blog.sockdrawer.org and was retrieved from the Internet Archive.