Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why get IllegalStateException when redirect in Servlet Filter?

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet filter and to try and narrow down the cause of the error, it does only two things: redirect and call doFilter(...). For some reason this keeps throwing the error: IllegalStateException. (I'm using Jetty) The code:



This throws the following error:


Here's the weird thing: it throws the exception but will still show "test-redirect.jsp". If I take out the "chain.doFilter(...)" statement, then it doesn't throw the error, but obviously also doesn't show my page. Any idea what's going on? Am i not allowed to redirect in a filter?
[ September 08, 2006: Message edited by: Bear Bibeault ]
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case anyone's wondering, here's the reason why:

When you send a redirect, you should NOT continue the chain. A redirect message/request will then be sent to the client via HTTP headers, basically requesting that they request this new page ("test-redirect.jsp" in this example). Since browsers automatically do this, it then returns to the filter which now should continue the chain.

The correct code then:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Bizman:

When you send a redirect, you should NOT continue the chain.


Exactly. Once you have redirected or forwarded in any resource. filter or otherwise, you should do nothing else. Conventionally, a redirect or forward is either followed by a return statement, or is the last statement in a conditional.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic