How can I change the request uri by using Filter and pass it to the next Filter?

Viewed 89

I want to change the request URI from /aaa/bbb/ccc to /bbb/ccc. In other words, I want to remove the first part of the request URI.

I tried using the following Filter, but it does not work.

public class AnyRequestHandlerFilter implements Filter, Ordered {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        System.out.println(httpRequest.getRequestURI());
        String path = httpRequest.getRequestURI();
        if (path.contains("/aaa")) {
            log.info("front:{}", path);
            path = path.replace("/aaa/", "/");
            log.info("back:{}", path);
            httpRequest.getRequestDispatcher(path).forward(httpRequest, servletResponse);
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}
0 Answers
Related