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

Servlet Filter Confusion

Ram Chhabra
Ranch Hand

Joined: Jan 07, 2008
Posts: 48

I have created one filter. What i want that once control come to Filter, i need to do some validations and if it passes that validation then only i need to call chain.doFilter(res, req) but if validation fails then i don't want that request goes to servlet.

Can i send response from Filter only to client, without going to specific servlet.?

Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
I think you can do it this way...


Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Yes. you should be able to control if the filter should continue the response or not.

So much trouble in the world -- Bob Marley
Ram Chhabra
Ranch Hand

Joined: Jan 07, 2008
Posts: 48
Thanks for your reply. Inspite of sending to error page, is it ok to create the Response object and send back to client from filter only.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

I would not generate output in a filter -- that's not its intent. Rather forward or redirect to whatever resource (JSP or servlet) will gneerate the desired response.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Servlet Filter Confusion
jQuery in Action, 3rd edition