I have a buzz to solve and need some help. Suppose I have a URL from my domain, something like: http://mydomain.com/any_page.xhtml for example. I’d like to intercept the user request when clicking the link, that theoretically directs to my domain and need it to intercept and redirect it to determined new URL based in my criterion.
I’m working with simple Servlets. During my investigation I’ve seen that Filter may help me.
Does anybody know how to create something for this proposal?
I have a buzz to solve and need some help. Suppose I have a
Share
Just implement
javax.servlet.Filter.If you map this on an URL pattern of
/*it will be executed on every request.or when you’re already on Servlet 3.0
You can obtain the request URI by
HttpServletRequest#getRequestURI()in filter’sdoFilter()method as follows:You can use any of the methods provided by
java.lang.Stringclass to compare/manipulate it.You can use
if/elsekeywords provided by the Java language to control the flow in code.You can use
HttpServletResponse#sendRedirect()to send a redirect.You can use
FilterChain#doFilter()to just let the request continue.Do the math. You can of course also use a 3rd party one, like Tuckey’s URL rewrite filter which is, say, the Java variant of Apache HTTPD’s
mod_rewrite.See also: