aspose file tools*
The moose likes Servlets and the fly likes filter question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "filter question" Watch "filter question" New topic
Author

filter question

Steve Mutonshi
Greenhorn

Joined: Jun 13, 2008
Posts: 13
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

Joined: Jan 10, 2002
Posts: 61772
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Mutonshi
Greenhorn

Joined: Jun 13, 2008
Posts: 13
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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



[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
@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.


Thanks and Regards
Steve Mutonshi
Greenhorn

Joined: Jun 13, 2008
Posts: 13
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

Joined: Dec 11, 2004
Posts: 13410

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).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Steve Mutonshi
Greenhorn

Joined: Jun 13, 2008
Posts: 13
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
@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
 
subject: filter question