wood burning stoves 2.0*
The moose likes Servlets and the fly likes chain.doFilter() being called twice. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "chain.doFilter() being called twice." Watch "chain.doFilter() being called twice." New topic
Author

chain.doFilter() being called twice.

chandrakant karale
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
While tracing the root cause for IllegalStateException, I encountered following code for a gzip filter.
I suspect that chain.doFilter() should not be called multiple times in any flow, and there should be a return statement after wrappedResponse.finishResponse(); .
Please comment on this; I am new to filters.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Without knowing more, I'd agree.
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

of course here chain.doFilter can be called twice . you can correct the logic by using a boolean flag which will take care of it .
for ex:


avi sinha


SCJP 5.0 SCWCD 5.0
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

For me, adding a flag for this purpose unnecessarily complicates the method and makes it harder to understand.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

David Newton wrote:For me, adding a flag for this purpose unnecessarily complicates the method and makes it harder to understand.


I agree. Just write it as a series of if's and else's. And if you get rid of this line:

which surely can't represent the real-life requirement to have a filter which expects both HTTP requests and non-HTTP requests, it makes the if-else code much more obvious.
cow clarke
Greenhorn

Joined: Nov 12, 2009
Posts: 15
The times being called of this method depends on the web.xml config file on my opinion.
 
Don't get me started about those stupid light bulbs.
 
subject: chain.doFilter() being called twice.
 
Similar Threads
Filters and GZip
Stream and writer
Fudging Filter Functionality as a method call
Filter and Struts
JSP with large data loads slow in Solaris10 OS , we are using Jrun4 and nested logic:iterate