You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Scott Purcell <sp...@vertisinc.com> on 2005/04/27 16:19:40 UTC

Filter Being Called Repeatedly

Hello,
I decided to try and implement a filter for my struts application. A filter that would check for a session object, and if it does not exist, send the user to a certain link.

Anyway, I found an example in the "Struts Cookbook" and I am implementing it. But for whatever reason, when I hit a page that should redirect me, I can see (by debug statements) that the filter is hit 10 or more times, and then I get an error stating: "Redirect limit for this URL exceeeded. Unable to load the requested page") in an alert Box?

I would really like to get this going:
Here is the code, and the web.xml I am using. Any help would be appreciated.

CODE
public class AppFilter implements Filter {

  private String onFailure = "index.jsp";
  private FilterConfig filterConfig;

  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    onFailure = filterConfig.getInitParameter("onFailure");
  }

  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain)
    throws IOException, ServletException {


    Logger log = Logger.getLogger(this.getClass().getName());
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    log.info("XXXXXXXXXXXXX");
    log.info("AppFilter called for a request.");
    log.info("param onFailure=" + onFailure);

    // if the page is onFailure page continue down the chain
    if (req.getServletPath().equals(onFailure)) {
      log.info("YYYY equals YYYY");
      chain.doFilter(request, response);
      return;
    }


    HttpSession session = req.getSession();
    AppObject appObject = (AppObject)session.getAttribute("APP_OBJECT");
    if (appObject == null) {
      log.info("AppObject is null, send to front door.");
      log.info("sending path=" + req.getContextPath() + onFailure);
      resp.sendRedirect(req.getContextPath() + onFailure);
      return;
    } else {
      log.info("DDDDDDDD");
      chain.doFilter(request, response);
    }
  }

  public void destroy() {
  }
}


WEB.XML
<filter>
    <filter-name>AppFilter</filter-name>
    <filter-class>com.mb.purcell.app.AppFilter</filter-class>
    <init-param>
      <param-name>onFailure</param-name>
      <param-value>/index.jsp</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>AppFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Thanks,

Scott K Purcell

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Filter Being Called Repeatedly

Posted by Tom Ziemer <t....@dkfz-heidelberg.de>.
Hi,

I just looked briefly at your code, but could it be, that your filter 
intercepts the calls to the error page as well? So you would be 
redirecting, then interceptiong, redirecting, etc.

Maybe you could try a different URL pattern for the filter - something 
like /protected/*

Hope this helps.

Regards,
Tom

Scott Purcell wrote:
> Hello,
> I decided to try and implement a filter for my struts application. A filter that would check for a session object, and if it does not exist, send the user to a certain link.
> 
> Anyway, I found an example in the "Struts Cookbook" and I am implementing it. But for whatever reason, when I hit a page that should redirect me, I can see (by debug statements) that the filter is hit 10 or more times, and then I get an error stating: "Redirect limit for this URL exceeeded. Unable to load the requested page") in an alert Box?
> 
> I would really like to get this going:
> Here is the code, and the web.xml I am using. Any help would be appreciated.
> 
> CODE
> public class AppFilter implements Filter {
> 
>   private String onFailure = "index.jsp";
>   private FilterConfig filterConfig;
> 
>   public void init(FilterConfig filterConfig) throws ServletException {
>     this.filterConfig = filterConfig;
>     onFailure = filterConfig.getInitParameter("onFailure");
>   }
> 
>   public void doFilter(ServletRequest request,
>                        ServletResponse response,
>                        FilterChain chain)
>     throws IOException, ServletException {
> 
> 
>     Logger log = Logger.getLogger(this.getClass().getName());
>     HttpServletRequest req = (HttpServletRequest) request;
>     HttpServletResponse resp = (HttpServletResponse) response;
>     log.info("XXXXXXXXXXXXX");
>     log.info("AppFilter called for a request.");
>     log.info("param onFailure=" + onFailure);
> 
>     // if the page is onFailure page continue down the chain
>     if (req.getServletPath().equals(onFailure)) {
>       log.info("YYYY equals YYYY");
>       chain.doFilter(request, response);
>       return;
>     }
> 
> 
>     HttpSession session = req.getSession();
>     AppObject appObject = (AppObject)session.getAttribute("APP_OBJECT");
>     if (appObject == null) {
>       log.info("AppObject is null, send to front door.");
>       log.info("sending path=" + req.getContextPath() + onFailure);
>       resp.sendRedirect(req.getContextPath() + onFailure);
>       return;
>     } else {
>       log.info("DDDDDDDD");
>       chain.doFilter(request, response);
>     }
>   }
> 
>   public void destroy() {
>   }
> }
> 
> 
> WEB.XML
> <filter>
>     <filter-name>AppFilter</filter-name>
>     <filter-class>com.mb.purcell.app.AppFilter</filter-class>
>     <init-param>
>       <param-name>onFailure</param-name>
>       <param-value>/index.jsp</param-value>
>     </init-param>
>   </filter>
> 
>   <filter-mapping>
>     <filter-name>AppFilter</filter-name>
>     <url-pattern>/*</url-pattern>
>   </filter-mapping>
> 
> Thanks,
> 
> Scott K Purcell
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Filter Being Called Repeatedly

Posted by Dave Newton <ne...@pingsite.com>.
Scott Purcell wrote:

>    if (req.getServletPath().equals(onFailure)) {
>  
>
[...]

>      resp.sendRedirect(req.getContextPath() + onFailure);
>  
>
Are you sure that getServletPath() and getContextPath() + onFailure are 
the same thing? My impression is that getServletPath() may not be 
returning what you think it is; check the J2EE JavaDocs and see if 
that's really what you want.

Also, might as well create a class logger rather than instantiating one 
every time through.

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Filter Being Called Repeatedly

Posted by David Evans <ds...@berndtgroup.net>.
Hello,

comments below

On Wed, 2005-04-27 at 10:19, Scott Purcell wrote:
> Hello,
> I decided to try and implement a filter for my struts application. A filter that would check for a session object, and if it does not exist, send the user to a certain link.
> 
> Anyway, I found an example in the "Struts Cookbook" and I am implementing it. But for whatever reason, when I hit a page that should redirect me, I can see (by debug statements) that the filter is hit 10 or more times, and then I get an error stating: "Redirect limit for this URL exceeeded. Unable to load the requested page") in an alert Box?
> 
> I would really like to get this going:
> Here is the code, and the web.xml I am using. Any help would be appreciated.
> 
> CODE
> public class AppFilter implements Filter {
> 
>   private String onFailure = "index.jsp";
>   private FilterConfig filterConfig;
> 
>   public void init(FilterConfig filterConfig) throws ServletException {
>     this.filterConfig = filterConfig;
>     onFailure = filterConfig.getInitParameter("onFailure");
>   }
> 
>   public void doFilter(ServletRequest request,
>                        ServletResponse response,
>                        FilterChain chain)
>     throws IOException, ServletException {
> 
> 
>     Logger log = Logger.getLogger(this.getClass().getName());
>     HttpServletRequest req = (HttpServletRequest) request;
>     HttpServletResponse resp = (HttpServletResponse) response;
>     log.info("XXXXXXXXXXXXX");
>     log.info("AppFilter called for a request.");
>     log.info("param onFailure=" + onFailure);
> 
>     // if the page is onFailure page continue down the chain
i think you should change this:
>     if (req.getServletPath().equals(onFailure)) {
to this:
      if (req.getPathInfo().equals(onFailure)) {
>       log.info("YYYY equals YYYY");
>       chain.doFilter(request, response);
>       return;
>     }
> 
> 
>     HttpSession session = req.getSession();
>     AppObject appObject = (AppObject)session.getAttribute("APP_OBJECT");
>     if (appObject == null) {
>       log.info("AppObject is null, send to front door.");
>       log.info("sending path=" + req.getContextPath() + onFailure);
>       resp.sendRedirect(req.getContextPath() + onFailure);
>       return;
>     } else {
>       log.info("DDDDDDDD");
>       chain.doFilter(request, response);
>     }
>   }
> 
>   public void destroy() {
>   }
> }
> 
> 
> WEB.XML
> <filter>
>     <filter-name>AppFilter</filter-name>
>     <filter-class>com.mb.purcell.app.AppFilter</filter-class>
>     <init-param>
>       <param-name>onFailure</param-name>
>       <param-value>/index.jsp</param-value>
>     </init-param>
>   </filter>
> 
>   <filter-mapping>
>     <filter-name>AppFilter</filter-name>
>     <url-pattern>/*</url-pattern>
>   </filter-mapping>
> 
> Thanks,
> 
> Scott K Purcell
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Filter Being Called Repeatedly

Posted by gd...@cmhc-schl.gc.ca.
Oh yes... I carefully looked at the code.
Apologies for misleading the list. 

- Glenn




Dave Newton <ne...@pingsite.com> 
27/04/2005 10:42 AM
Please respond to
"Struts Users Mailing List" <us...@struts.apache.org>


To
Struts Users Mailing List <us...@struts.apache.org>
cc

Subject
Re: Filter Being Called Repeatedly
Classification








gdeschen@cmhc-schl.gc.ca wrote:

>Umh... I believe that you don't need the  return after the chain.doFilter
> 
>
You do, otherwise the remaining code in the filter will be executed 
after the request is finished being processed: filters _wrap_ a request. 
This is how a gzipping filter works, for example.

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org




Re: Filter Being Called Repeatedly

Posted by Dave Newton <ne...@pingsite.com>.
gdeschen@cmhc-schl.gc.ca wrote:

>Umh... I believe that you don't need the  return after the chain.doFilter
>  
>
You do, otherwise the remaining code in the filter will be executed 
after the request is finished being processed: filters _wrap_ a request. 
This is how a gzipping filter works, for example.

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Filter Being Called Repeatedly

Posted by gd...@cmhc-schl.gc.ca.
Umh... I believe that you don't need the  return after the chain.doFilter

    if (req.getServletPath().equals(onFailure)) {
      log.info("YYYY equals YYYY");
      chain.doFilter(request, response);
remove -->   return;
    }

HTH,
Glenn




"Scott Purcell" <sp...@vertisinc.com> 
27/04/2005 10:19 AM
Please respond to
"Struts Users Mailing List" <us...@struts.apache.org>


To
<us...@struts.apache.org>
cc

Subject
Filter Being Called Repeatedly
Classification








Hello,
I decided to try and implement a filter for my struts application. A 
filter that would check for a session object, and if it does not exist, 
send the user to a certain link.

Anyway, I found an example in the "Struts Cookbook" and I am implementing 
it. But for whatever reason, when I hit a page that should redirect me, I 
can see (by debug statements) that the filter is hit 10 or more times, and 
then I get an error stating: "Redirect limit for this URL exceeeded. 
Unable to load the requested page") in an alert Box?

I would really like to get this going:
Here is the code, and the web.xml I am using. Any help would be 
appreciated.

CODE
public class AppFilter implements Filter {

  private String onFailure = "index.jsp";
  private FilterConfig filterConfig;

  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    onFailure = filterConfig.getInitParameter("onFailure");
  }

  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain)
    throws IOException, ServletException {


    Logger log = Logger.getLogger(this.getClass().getName());
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    log.info("XXXXXXXXXXXXX");
    log.info("AppFilter called for a request.");
    log.info("param onFailure=" + onFailure);

    // if the page is onFailure page continue down the chain
    if (req.getServletPath().equals(onFailure)) {
      log.info("YYYY equals YYYY");
      chain.doFilter(request, response);
      return;
    }


    HttpSession session = req.getSession();
    AppObject appObject = (AppObject)session.getAttribute("APP_OBJECT");
    if (appObject == null) {
      log.info("AppObject is null, send to front door.");
      log.info("sending path=" + req.getContextPath() + onFailure);
      resp.sendRedirect(req.getContextPath() + onFailure);
      return;
    } else {
      log.info("DDDDDDDD");
      chain.doFilter(request, response);
    }
  }

  public void destroy() {
  }
}


WEB.XML
<filter>
    <filter-name>AppFilter</filter-name>
    <filter-class>com.mb.purcell.app.AppFilter</filter-class>
    <init-param>
      <param-name>onFailure</param-name>
      <param-value>/index.jsp</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>AppFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Thanks,

Scott K Purcell

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org