aspose file tools*
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
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: 453

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: 18909
    
    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.
 
 
subject: chain.doFilter() being called twice.