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 import java.io.IOException; 21 22 import javax.servlet.Filter; 23 import javax.servlet.FilterChain; 24 import javax.servlet.FilterConfig; 25 import javax.servlet.ServletException; 26 import javax.servlet.ServletRequest; 27 import javax.servlet.ServletResponse; 28 import javax.servlet.http.HttpServletRequest; 29 30 /* ------------------------------------------------------------ */ 31 /** Welcome Filter 32 * This filter can be used to server an index file for a directory 33 * when no index file actually exists (thus the web.xml mechanism does 34 * not work). 35 * 36 * This filter will dispatch requests to a directory (URLs ending with /) 37 * to the welcome URL determined by the "welcome" init parameter. So if 38 * the filter "welcome" init parameter is set to "index.do" then a request 39 * to "/some/directory/" will be dispatched to "/some/directory/index.do" and 40 * will be handled by any servlets mapped to that URL. 41 * 42 * Requests to "/some/directory" will be redirected to "/some/directory/". 43 */ 44 public class WelcomeFilter implements Filter 45 { 46 private String welcome; 47 48 public void init(FilterConfig filterConfig) 49 { 50 welcome=filterConfig.getInitParameter("welcome"); 51 if (welcome==null) 52 welcome="index.html"; 53 } 54 55 /* ------------------------------------------------------------ */ 56 public void doFilter(ServletRequest request, 57 ServletResponse response, 58 FilterChain chain) 59 throws IOException, ServletException 60 { 61 String path=((HttpServletRequest)request).getServletPath(); 62 if (welcome!=null && path.endsWith("/")) 63 request.getRequestDispatcher(path+welcome).forward(request,response); 64 else 65 chain.doFilter(request, response); 66 } 67 68 public void destroy() {} 69 } 70