1 // ======================================================================== 2 // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd. 3 // ------------------------------------------------------------------------ 4 // All rights reserved. This program and the accompanying materials 5 // are made available under the terms of the Eclipse Public License v1.0 6 // and Apache License v2.0 which accompanies this distribution. 7 // The Eclipse Public License is available at 8 // http://www.eclipse.org/legal/epl-v10.html 9 // The Apache License v2.0 is available at 10 // http://www.opensource.org/licenses/apache2.0.php 11 // You may elect to redistribute this code under either of these licenses. 12 // ======================================================================== 13 14 package org.eclipse.jetty.servlets; 15 import java.io.IOException; 16 17 import javax.servlet.Filter; 18 import javax.servlet.FilterChain; 19 import javax.servlet.FilterConfig; 20 import javax.servlet.ServletException; 21 import javax.servlet.ServletRequest; 22 import javax.servlet.ServletResponse; 23 import javax.servlet.http.HttpServletRequest; 24 25 /* ------------------------------------------------------------ */ 26 /** Welcome Filter 27 * This filter can be used to server an index file for a directory 28 * when no index file actually exists (thus the web.xml mechanism does 29 * not work). 30 * 31 * This filter will dispatch requests to a directory (URLs ending with /) 32 * to the welcome URL determined by the "welcome" init parameter. So if 33 * the filter "welcome" init parameter is set to "index.do" then a request 34 * to "/some/directory/" will be dispatched to "/some/directory/index.do" and 35 * will be handled by any servlets mapped to that URL. 36 * 37 * Requests to "/some/directory" will be redirected to "/some/directory/". 38 */ 39 public class WelcomeFilter implements Filter 40 { 41 private String welcome; 42 43 public void init(FilterConfig filterConfig) 44 { 45 welcome=filterConfig.getInitParameter("welcome"); 46 if (welcome==null) 47 welcome="index.html"; 48 } 49 50 /* ------------------------------------------------------------ */ 51 public void doFilter(ServletRequest request, 52 ServletResponse response, 53 FilterChain chain) 54 throws IOException, ServletException 55 { 56 String path=((HttpServletRequest)request).getServletPath(); 57 if (welcome!=null && path.endsWith("/")) 58 request.getRequestDispatcher(path+welcome).forward(request,response); 59 else 60 chain.doFilter(request, response); 61 } 62 63 public void destroy() {} 64 } 65