Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the requestURL using filter or servlet

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"

like image 303
user1497911 Avatar asked Sep 29 '12 13:09

user1497911


People also ask

What is the difference between filter and servlet?

Filter provides functionality which can be “attached” to any web resource. Servlet used for performing action which needs for particular request as user login, get response based on user role, interacts with database for getting data, business logic execution, and more.

How do I change the URL of a Java request?

Use either ServletRequest#getRequestDispatcher() and then RequestDispatcher#forward() to forward the request/response to the new URL (server-side redirect, not reflected in browser address bar), or cast the incoming ServletResponse to HttpServletResponse and then HttpServletResponse#sendRedirect() to redirect the ...

What is the use of filter in servlet?

The Java Servlet specification version 2.3 introduces a new component type, called a filter. A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses.


2 Answers

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.

like image 69
Tomasz Nurkiewicz Avatar answered Oct 10 '22 03:10

Tomasz Nurkiewicz


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()
}
like image 38
user3191192 Avatar answered Oct 10 '22 01:10

user3191192