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

How to redirect through filter chain

Arun Kumar
Ranch Hand

Joined: May 22, 2006
Posts: 62
I have an application and for every request it passes through a chain of filters. Depending upon certain condition I want the reesponse to break out of a filter and get forwarded to certain url how can I achieve this.

I have tried


both does not work.

I even tried


but it does not break out of the filter chain and executes doChain() so effectively what happens that it executes the page where i want it to be redirected and goes back again to the location redirected by the filter.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
you break out of the filter chain by not calling filterchain.doFilter(). You will wind up in the resource that you originally requested (Servelt of JSP)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

both does not work.

This is not a useful description of what is happening.

You should certainly be able to either forward or redirect from a filter.

[Asking smart questions] [About Bear] [Books by Bear]
Arun Kumar
Ranch Hand

Joined: May 22, 2006
Posts: 62
Actully i can see the forward and redirect both working as i have put in the sop's in the script of the page but...........after that is done it starts executing the filter chain and which takes it to the other url which was originally invoked and for which the filter chain were working.

My intention is that the processing should stop after the forwarded/redirected page is served.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

The forward and sendRedirect methods are no different than any other Java method call. They have no way of halting the execution of their calling method.

You will need to structure your code in such a way that either forward/sendRedirect OR doFilter is called.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Arun Kumar
Ranch Hand

Joined: May 22, 2006
Posts: 62
Thanks Ben!!

This is helpful.........I was conceptually wrong........I thought that once the the redirction or forward is executed it will break out of filter chain on it's own.

I have now made the changes.

Appreciate your help.
I agree. Here's the link:
subject: How to redirect through filter chain
It's not a secret anymore!