1 // ======================================================================== 2 // Copyright 199-2004 Mort Bay Consulting Pty. Ltd. 3 // ------------------------------------------------------------------------ 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 // ======================================================================== 14 15 package org.mortbay.servlet; 16 import java.io.IOException; 17 18 import javax.servlet.Filter; 19 import javax.servlet.FilterChain; 20 import javax.servlet.FilterConfig; 21 import javax.servlet.ServletException; 22 import javax.servlet.ServletRequest; 23 import javax.servlet.ServletResponse; 24 import javax.servlet.http.HttpServletRequest; 25 26 /* ------------------------------------------------------------ */ 27 /** Welcome Filter 28 * This filter can be used to server an index file for a directory 29 * when no index file actually exists (thus the web.xml mechanism does 30 * not work). 31 * 32 * This filter will dispatch requests to a directory (URLs ending with /) 33 * to the welcome URL determined by the "welcome" init parameter. So if 34 * the filter "welcome" init parameter is set to "index.do" then a request 35 * to "/some/directory/" will be dispatched to "/some/directory/index.do" and 36 * will be handled by any servlets mapped to that URL. 37 * 38 * Requests to "/some/directory" will be redirected to "/some/directory/". 39 */ 40 public class WelcomeFilter implements Filter 41 { 42 private String welcome; 43 44 public void init(FilterConfig filterConfig) 45 { 46 welcome=filterConfig.getInitParameter("welcome"); 47 if (welcome==null) 48 welcome="index.html"; 49 } 50 51 /* ------------------------------------------------------------ */ 52 public void doFilter(ServletRequest request, 53 ServletResponse response, 54 FilterChain chain) 55 throws IOException, ServletException 56 { 57 String path=((HttpServletRequest)request).getServletPath(); 58 if (welcome!=null && path.endsWith("/")) 59 request.getRequestDispatcher(path+welcome).forward(request,response); 60 else 61 chain.doFilter(request, response); 62 } 63 64 public void destroy() {} 65 } 66