a) if the code runs "filterChain.doFilter", will it directly leave the current line or continue to run the rest of codes in this "doFilter" method ?
b) similarly, if the code runs line 2 "dispath.forward(request, response)" will it directly leave line 2 or continue to run the rest of codes in this "doFilter" method ?
c) My filter applies to all servlet and .jsp. So when it is forwarded to "output.jsp", it will still goes through this "doFilter" method first, is that right ? Then it sounds like an endless loop..
How about "doFilter" ? I think it goes to the next filter. but if I have no other filter, will it directly go to the destination page ? or will it goes through this filter again before going to the next page ? Then it looks like another endless loop... Really confused.
Anybody could help me out ? please reply to each question separately so I can distinguish better. Thanks !
Filters are Java classes just like any other. So there can be no magic relaxation of the rules. After the doFilter method completes, control is returned back to the filter.
When the last filter has executed, doFilter will execute the target resources; again, no endless loop.
It'd be easy to set up a small test to try all this out.
And by the way, please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and many people that might be able to help you will just move along. Please read this for more information.
You can go back and change your post to add code tags by clicking the . [ June 13, 2008: Message edited by: Ben Souther ]
I see. So "doFilter()" just do the next filtering stuff. And if you don't have any filter after the current one, the code will just move to the next line in the current method..
But, how about the line 2 in my code ? it was "forward" to a JSP page. Since my filter intercepts all JSP page, before I can get to that JSP, doesn't it come to this filter class first again ?
yes , there are some pages for which you do not want filtering .. like session manage apps, when seesion ends a user should be redireced to , say "login.jsp" ..
Since we're redirecting to login page from this filter, if we don't disable session control for it, filter will again redirect to it and this will be result with an infinite loop...
But, how about the line 2 in my code ? it was "forward" to a JSP page. Since my filter intercepts all JSP page, before I can get to that JSP, doesn't it come to this filter class first again ?
You are correct here in the sense that this would result in an infinite loop, but I think the design you are following is not correct. A filter which intercepts a particular page must not forward to the same page.
Thanks and Regards
Steve Mutonshi
Greenhorn
Joined: Jun 13, 2008
Posts: 13
posted
0
Originally posted by Satya Maheshwari: @Steve Mutonshi
You are correct here in the sense that this would result in an infinite loop, but I think the design you are following is not correct. A filter which intercepts a particular page must not forward to the same page.
Eaxctly. I tried to do "if condition is met, forward to another JSP page". However, since my filter applies to all JSP page, it will have to go through this filter again. What a problem ! Any idea how to bypass it ?
Originally posted by Ben Souther: Filters will only be invoked on forwards if configured to do so. Look at SRV.6.2.5 in the servlet spec (link in my signature).
Yes, I just found that. But if you specify your filter to be applied to /*.jsp, then even you don't add "FORWARD" in the web.xml filter config, it still applies to it.
Is there a way for me to tell the filter to exclude a certain page in web.xml ? guess not.
Satya Maheshwari
Ranch Hand
Joined: Jan 01, 2007
Posts: 368
posted
0
@Mutonshi
But if you specify your filter to be applied to /*.jsp, then even you don't add "FORWARD" in the web.xml filter config, it still applies to it.
You can explicitly prevent the 'FORWARD' from invoking the filter by not specifying it and specifying others in web.xml.
If you do not specify any dispatcher, the filter is always applied.