2017-03-23 33 views
0

我做了一个过滤器,它负责将.do的请求,并且是首次宣布过滤器存在过滤URL效果:过滤和RequestDispatcher的改变上

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

    if(! (request instanceof HttpServletRequest)){ 
     chain.doFilter(request, response); 
    } 

    HttpServletRequest httpServletRequest = (HttpServletRequest) request; 
    String requestUrl = httpServletRequest.getServletPath(); 

    if(!isBetaPathRequest(httpServletRequest) || StringUtils.contains(requestUrl,".")){ 
     chain.doFilter(request, response); 
     return; 
    } 

    String newUrl = requestUrl; 

    int lastIndexOf = requestUrl.lastIndexOf("?"); 
    if(lastIndexOf == -1){ 

     if(requestUrl.charAt(requestUrl.length()-1) != '/'){ 
     newUrl = newUrl.concat(".do"); 
     } 

    } 
    else{ 
     newUrl = requestUrl.substring(0, lastIndexOf-1) .concat(".do") .concat(requestUrl.substring(lastIndexOf)); 
    } 

    final RequestDispatcher rq = getRequestDispatcher(request, newUrl); 
    rq.forward(request, response); 
    } 

还有其他各种过滤器项目也宣告web.xml文件,我与调试器通知他们没有运行。

当前映射一个重要滤波器是例如:

ContentResponseCachingFilter *。做

但初始传入的请求不包括在URL .do,直到它通过BetaRewriteUrlFilter通过。

当调用RequestDispatcher.forward时,是否应用了过滤器,还是需要手动调用它们,或者使用302重定向?

回答

0

我最终做的是将这个新过滤器放在web.xml定义中。

该重写过滤器需要在链上最后,因为使用方法不会破坏功能或禁止其他过滤器的运行。