File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Filters in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Filters in Servlets" Watch "Filters in Servlets" New topic

Filters in Servlets

Dean Jones

Joined: Jun 18, 2002
Posts: 24
hi can any one of u tell me wat r the filters in servlet request and response and can u send me a example how they can be actually used
Ken Robinson
Ranch Hand

Joined: Jul 25, 2002
Posts: 52
Filters are not part of the Request/Response. They are entities all to themselves, much like Servlet.
Filters, in my opinion, have to potential to totally change how web development is done. A Filter is much like a Servlet. It has an init and a doFilter. The init is like the init in a Servlet. The doFilter is like the doPost/doGet of the Servlet. The doFilter's big difference is that it, in addition to the ServletRequest/ServletResponse, takes a FitlerChain.
Basically, you code your Filters and then 'chain' them together or with other resource. If I code a SecurityFilter, via the web.xml I can ensure it runs before each protected Servlet/HTML/JPEG/JSP by simply mapping it to the same URL as the protected resource. The server automatically runs the Filter before the resource (or after if you code it that way).
If you think about it, you can code your entire web app as Filters/JSP. You code Filters as very small bits of logic and use the web.xml to run those you want before a requested JSP page.
For a quick, and probably much better, overview of Filters, Check This Out..
Hope this helps.
I agree. Here's the link:
subject: Filters in Servlets
It's not a secret anymore!