how to change the requestURL using filter or servlet

前端 未结 2 2150
孤城傲影
孤城傲影 2021-01-18 16:53

how to change the requestURL using filter or servlet .

for example if request is \"http://servername1:8080\" I want to change the same to \"http://servername2:7001

2条回答
  •  长情又很酷
    2021-01-18 17:11

    The above is a good solution compared to many others on the web that use .forward() as that breaks the filter chain. This solution allows subsequent filters to process the request after is has been modified.

    But there are 2 additional methods that must implemented in order for this to be 'transparent' to downstream filter processing, and they must present the same modification of the URL to present a consistent request object. The wrapper must implement:

    @Override
    public String getRequestURI() {
        final String originalUri = ((HttpServletRequest)getRequest()).getRequestURI();
        return "/"; // Must be consistent with getRequestURL()
    }
    @Override
    public String getServletPath() {
        final String originalPath = ((HttpServletRequest)getRequest()).getServletPath();
        return "/"; // Must be consistent with getRequestURL()
    }
    

提交回复
热议问题