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