views:

625

answers:

6

Very interesting tricks can be done with java servlet filters in security, performance, etc.

What are the best servlet filters out there?

+5  A: 

GZIP compression would get my vote.

duffymo
That's however also builtin in most servletcontainers. In for example Tomcat and clones you can just do `<Connector compression="on">` in `server.xml` to enable GZIP.
BalusC
+9  A: 

UrlRewriteFilter

Bozho
by popular vote
flybywire
+2  A: 

Hands down, the best, most insanely useful servlet filter out there is UrlRewriteFilter.

I contributed code to the Jython project that lets you write servlet filters in Jython.

Jonathan Feinberg
+4  A: 

WhitespaceFilter to trim all whitespace from generated HTML. It can save over 50% of bandwidth, regardless of whether GZIP is used.

The top bandwith-saving combo would thus be Gzipping and trimming whitespace. If you're using a Filter for GZIP as well instead of just configuring the servletcontainer to turn GZIP on (in for example Tomcat and clones by just doing <Connector compression="on"> in server.xml), then you need to ensure that the whitespace filter runs before the GZIP filter.

BalusC
You appear to be linking to your own code. Whitespace can be filtered on build without any run time performance hit.
Pool
since when it's wrong to link to own code? ;) What do you mean by "Filtered on build"? (p.s.sorry, out of upvotes for today)
Bozho
btw, BalusC, do you know what is the efficiency of gzipping the whitespaces - i.e. doesn't it have a similar bandwidth outcome?
Bozho
got my answer : http://stackoverflow.com/questions/855526/removing-extra-whitespace-from-generated-html-in-mvc :)
Bozho
@The Feast: should I have hosted it at code.google.com, sourceforge.net, github.com or so? :) Trimming whitespace on build is however a nice suggestion, although it doesn't seem to be as easy as with a filter (which is efficient enough). Do you have any resources on the subject?
BalusC
It's a bit immodest to post your own filter under "most useful java servlet filter" without at least acknowledging.
Pool
Modesty is overrated. We all reinvent the wheel sometimes, since a lot of code out there just plain sucks. So why not promote our yet-another-best-solution-to-this? :)
Photodeus
A: 

Different kinds of CharsetEncodingFilters.

axtavt
+1  A: 

This one impressed me, an implementation of the YUI CSS and Javascript compressor:

http://jango.wordpress.com/2009/04/27/js-minify-filter-in-java/

Jon
We also use YUI minifier, but not as filter. It makes no sense to minify on every request, it's only more expensive. We use it to minify the files just once during deploy time.
BalusC