Link to home
Start Free TrialLog in
Avatar of morsa804tons
morsa804tons

asked on

excluding an URI from a container

hi,

i've a java app deployed at tomcat, under uri https://xxxx.com/mag (/mag/*), but i need if some user types https://xxxx.com/mag/quer, it redirects to an index.html file. How i can do that?

Thanks!
Avatar of rrz
rrz
Flag of United States of America image

You could use a Filter.  
http://java.sun.com/javaee/5/docs/tutorial/doc/bnagb.html
http://java.sun.com/products/servlet/Filters.html   

package rrz;  
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RedirectFilter implements Filter {
  public void init(FilterConfig config) throws ServletException{
                         System.out.println("init of RedirectFilter"); // for testing
  }
  public void destroy(){}        
  public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain)
                   throws ServletException, IOException {
                          HttpServletRequest request = (HttpServletRequest)req;
                          String uri = request.getRequestURI();
                          if(uri.indexOf("whatever") == -1)chain.doFilter(req, resp);
                          else response.sendRedirect(request.getContextPath() + "/index.html");
  }
}

and in your web.xml you should have something like  
           <filter>
                    <filter-name>redirectfilter</filter-name>
                    <filter-class>rrz.RedirectFilter</filter-class>
           </filter>
           <filter-mapping>
                     <filter-name>redirectfilter</filter-name>
                     <url-pattern>(/mag/*</url-pattern>
           </filter-mapping>  
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial