Reputation: 35306
I need to forward all kind of request from the ROOT, i.e http://localhost:8080/ to http://localhost:8080/myRoot
Is it possible, do I need to create a redirect servlet? However, I think tomcat can be configured to behave that way?
Upvotes: 2
Views: 4754
Reputation: 35306
What I did is to use UrlRewriteFilter
Deployed the filter into the ROOT and modified the urlrewrite.xml
like this:
<rule>
<from>^/$</from>
<to type="redirect">%{context-path}/myRoot</to>
</rule>
And now it works.
Upvotes: 0
Reputation: 1109132
An easy way would be to create a filter something like this
@WebFilter("/*")
public class RedirectFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String uri = request.getRequestURI();
String query = request.getQueryString();
if (query != null) {
uri = uri + "?" + query;
}
response.setStatus(301);
response.setHeader("Location", "/myRoot" + uri);
// Can also use response.sendRedirect(), but this does 302 by default.
}
// ...
}
and put it in Tomcat/webapps/ROOT/WEB-INF/classes/com/example/RedirectFilter.class
.
If you're still using Tomcat 6.0 or older, then remove @WebFilter
annotation and create the web.xml
accordingly.
Upvotes: 2