【AgileTC】_正则匹配修改uri路径

  • 主要Pattern工具类和Matcher工具类来实现;

  • 1.在filter中获取uri路径:

    1
    2
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    String path = httpServletRequest.getServletPath();
  • 2.创建正则表达式类对象,正则表达式对象一般声明为静态常量:

    1
    private static final Pattern PATTERN = Pattern.compile("agile/\\w+/");
  • 3.将uri路径中的agile/\\w+/去掉,思路就是先将原uri路径中的agile/\\w+/全部替换成空格,然后再用字符串的trim()方法去掉字符串头尾的空格:

    1
    2
    Matcher matcher = PATTERN.matcher(path);
    String newPath = matcher.replaceAll("").trim();
  • 4.将请求转发到新的uri路径

    1
    request.getRequestDispatcher(newPath).forward(httpServletRequest, httpServletResponse);