• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Infinite execution when forwarding requests in filters

Hi,
im using filters for authentication.i want to forward the request to error page
if authentication fails.but i couldnt do forward/include/redirect .If do that ,infinite
execution occures and i get stackOverFlow exception.

can any body plz help me

Here is my code

public void doFilter( final ServletRequest request, final ServletResponse response, FilterChain chain ) throws IOException, ServletException
  {
         
      HttpServletRequest req = (HttpServletRequest)request ;  
      HttpServletResponse res=(HttpServletResponse) response;
      StringBuffer path=req.getRequestURL();
 
      boolean flag=loginValidator(req);
      
        
         if(path.indexOf("login")<=-1 || path.indexOf("Authenticator")<=-1 )
        {
          if(!flag)
          {
            // i want to forward request to some page
        }
        }
 
        
        chain.doFilter( request, response ) ;         
 }

0
ramsharma23
Asked:
ramsharma23
  • 6
  • 3
1 Solution
 
bloodredsunCommented:
Just check to see if the requested URL is the error page and if it is, don't do any forwarding in your filter but let it go through to the error page :-)
0
 
TimYatesCommented:
// change:

               if(path.indexOf("login")<=-1 || path.indexOf("Authenticator")<=-1 )

to:

               if(path.indexOf("login") >= 0 || path.indexOf("Authenticator") >= 0 || path.indexOf( "ErrorPage" ) >= 0 )

your error page will be calling the filter which will fail loginvalidation, which will send it to the error page, etc ;-)

I think...

Tim
0
 
TimYatesCommented:
hehehe...typed too much ;-)

AND

               if(path.indexOf("login") >= 0 || path.indexOf("Authenticator") >= 0 || path.indexOf( "ErrorPage" ) >= 0 )

should be...

               if(path.indexOf("login") < 0 || path.indexOf("Authenticator") < 0 || path.indexOf( "ErrorPage" ) < 0 )

sigh...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ramsharma23Author Commented:
no it is not a error page.and  im getting this error for all the pages.
0
 
TimYatesCommented:
I assume you have some code instead of

           // i want to forward request to some page

I would say that is the problem...
0
 
ramsharma23Author Commented:
No tim nothing,i just had a forward there.
0
 
TimYatesCommented:
Why not have a redirect rather than a forward?

        response.sendRedirect( request.getContextPath() + "/Welcome.jsp" ) ;
0
 
ramsharma23Author Commented:
Tim as u said ,forwarding to error page was the problem.and it again invokes filter and then error page.
adding     path.indexOf( "ErrorPage" ) < 0 solves my problem.

Tnx tim, Tnx bloodredsun
0
 
TimYatesCommented:
Why the B grade?  Just wondering is all...

Was my answer rubbish?  Incomplete?

Sorry about that :-(
0
 
TimYatesCommented:
And bloodredsun said the same thing...so don't you feel splitting the points would have been fairer?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now