title: Why my Http Filter makes an infinite loop?
body: <p>I want to implement a Http Filter to intercept some requests made to my website. I want to intercept request to <a href="http://www.mywebsite.com/referral/userid" rel="nofollow">http://www.mywebsite.com/referral/userid</a>. I write the following Filter class: </p>
  
  <pre><code>public class ReferralFilter implements Filter{
  
  
      @Override
      public void init(FilterConfig filterConfig) throws ServletException {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void doFilter(ServletRequest request, ServletResponse response,
              FilterChain chain) throws IOException, ServletException {
           HttpServletRequest httpRequest = (HttpServletRequest) request;
              String contextPath = ((HttpServletRequest) request).getContextPath();
              String requestURI = httpRequest.getRequestURI();
  
              requestURI = StringUtils.substringAfter(requestURI, contextPath);
              if(StringUtils.contains(requestURI, "/referral/")){
                  String referralIdentifier = StringUtils.substringAfter(requestURI, contextPath+"/referral/");
                  //Search the referral's user
                  if(referralIdentifier!=null &amp;&amp; referralIdentifier.length()==12){
  
                          String cookieKey = "website.referral";
                          String cookieValue = referralIdentifier;
                          Cookie cookie = new Cookie(cookieKey,cookieValue);
                          cookie.setMaxAge(60*60*24*365);
                          ((HttpServletResponse) response).addCookie(cookie);
                          //redirect to the home page angularJS app
                          requestURI = StringUtils.substringBeforeLast(requestURI,"/referral/")+"#";
  
  
  
                  }
  
  
              }
  
              String newURI = requestURI;
              request.getRequestDispatcher(newURI).forward(request, response);
  
      }
  
      @Override
      public void destroy() {
          // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>
  
  <p>Here is my configuration :</p>
  
  <pre><code>@Configuration
  @AutoConfigureAfter(CacheConfiguration.class)
  public class WebConfigurer implements ServletContextInitializer, EmbeddedServletContainerCustomizer {
  
      private final Logger log = LoggerFactory.getLogger(WebConfigurer.class);
  
      @Inject
      private Environment env;
  
      @Autowired(required = false)
      private MetricRegistry metricRegistry;
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          log.info("Web application configuration, using profiles: {}", Arrays.toString(env.getActiveProfiles()));
          EnumSet&lt;DispatcherType&gt; disps = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC);
  
          // some initializations
  
          initReferralFilter(servletContext,disps);
          log.info("Web application fully configured");
      }
  
  
  
      /**
       * Initializes the Referral filter.
       */
      private void initReferralFilter(ServletContext servletContext, EnumSet&lt;DispatcherType&gt; disps) {
          log.debug("Registering Referral Filter");
          FilterRegistration.Dynamic referralFilter = servletContext.addFilter("referralFilter", new ReferralFilter());
          referralFilter.addMappingForUrlPatterns(disps, true, "/referral/*");
             }
  
      // other methods
  }
  </code></pre>
  
  <p>When i try to access to <a href="http://www.mywebsite.com/referral/eRfdg54GDGd2" rel="nofollow">http://www.mywebsite.com/referral/eRfdg54GDGd2</a> my filter executes infinitely. Why ?</p>
  
  <p>Thanks</p>