This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

filter question

 
Steve Mutonshi
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I implemented a filter. Unfortunately since I just started learning I have some questions about it, hope someone in this forum can help me out.

************
public void doFilter(...,..) {

if(some condition) {
filterChain.doFilter(request, response); // line 1 }
... // some code

if(another condition) {
RequestDispather dispath = request.getRequestDispather("output.jsp");
dispath.forward(request, response); // line 2
}

... // some more codes
}
******************

First I have only one filter. My questions are

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 !
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64194
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Steve Mutonshi
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Sagar Rohankar
Ranch Hand
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Here i what i thought

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Steve Mutonshi
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.
 
Steve Mutonshi
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Steve Mutonshi
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic