0

I have a scenario that when user opens http://MYWEBSITE.com/abc/ , the user is directed to xyz.html page which is in abc subdirectory. I am using Java for web development. How can I do this in web.xml?

P.S. URL is http://MYWEBSITE.com/abc/ not http://MYWEBSITE.com/abc

user958414
  • 385
  • 1
  • 5
  • 10

1 Answers1

0

you could define a servlet-filter in web.xml like this

<filter>
    <filter-name>incompleteUrlFilter</filter-name>
    <filter-class>com.mywebsite.IncompleteUrlFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>incompleteUrlFilter</filter-name>
    <url-pattern>/abc/*</url-pattern>
</filter-mapping>

and the filter class would look like this

package com.mywebsite;

public class IncompleteUrlFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        if( ((HttpServletRequest)req).getPathInfo() == null ){ // nothing after /abc/ ! 
            req.getRequestDispatcher("/abc/xyz.html").forward(req, resp); // forward to xyz.html
        } else {
            chain.doFilter(req, resp); // else continue as usual
        }
    }

}
jambriz
  • 1,273
  • 1
  • 10
  • 25