Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to redirect through filter chain

 
Arun Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

dispatcher.forward(..)
response.sendDirect(..)

both does not work.

I even tried

response.flushBuffer()

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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 65114
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Arun Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic