【问题标题】:Filter & RequestDispatcher to change URL effect on existing filters过滤器和 RequestDispatcher 更改现有过滤器的 URL 效果
【发布时间】:2017-08-16 09:47:26
【问题描述】:

我制作了一个过滤器,负责将.do 添加到请求中,并且是第一个声明的过滤器:

 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 文件中还声明了其他各种过滤器,我通过调试器通知它们没有运行。

一个重要过滤器的当前映射例如:

内容响应缓存过滤器 *。做

但最初的传入请求在通过BetaRewriteUrlFilter 之前不包括在 URl 中的.do

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

【问题讨论】:

    标签: servlets web.xml servlet-filters


    【解决方案1】:

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

    这个重写过滤器需要在链上的最后一个,因为使用request dispatcher forward 方法不会破坏功能或抑制其他过滤器的运行。

    【讨论】:

      猜你喜欢
      • 2017-12-12
      • 2014-05-06
      • 1970-01-01
      • 2012-02-15
      • 2013-04-09
      • 1970-01-01
      • 2011-04-24
      • 2017-07-29
      • 1970-01-01
      相关资源
      最近更新 更多