リクエストをインターセプトするフィルタを作成できます。フィルタでは、リクエスト URL が「/」であるかどうかを確認し、そうであれば、リクエストをウェルカム ページに転送します。
public class MyFilter implements Filter {
private ServletContext servletContext;
public void init(FilterConfig config) throws ServletException {
servletContext = config.getServletContext();
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String path = ((HttpServletRequest)request).getPathInfo();
if(path.equals("/")){
servletContext.getRequestDispatcher("/welcome.jsp").forward(request, response);
} else {
chain.doFilter(request,response);
}
}
}
web.xml でフィルターを適用します。
<filter>
<filter-name>welcomeFilter</filter-name>
<filter-class>the filter class</filter-class>
</filter>
<filter-mapping>
<filter-name>welcomeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>