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
Add the following servlet filter to your application:
public class RequestUrlRewritingFilter implements Filter {
//Empty init()/destroy() here
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(request) {
@Override
public StringBuffer getRequestURL() {
final StringBuffer originalUrl = ((HttpServletRequest) getRequest()).getRequestURL();
return new StringBuffer("http://servername2:7001");
}
};
chain.doFilter(wrapped, response);
}
}
All requests you want to intercept must go through it. As you can see it takes original request method and overrides getRequestURL() method by returning a different value. You still have access to the original request if you want to base new URL on the old one.
At the end you must continue processing the request chain.doFilter() but by providing wrapped request, not the original one.