1 22 package org.jboss.web.tomcat.servlets; 23 24 import java.io.IOException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.http.HttpServlet ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.ServletContext ; 30 import javax.servlet.RequestDispatcher ; 31 import javax.servlet.ServletConfig ; 32 33 import org.jboss.logging.Logger; 34 35 47 public class ContextForwardServlet extends HttpServlet 48 { 49 private static Logger log = Logger.getLogger(ContextForwardServlet.class); 50 51 private String forwardContext = "/error-pages"; 52 53 public void init(ServletConfig config) throws ServletException 54 { 55 super.init(config); 56 String param = config.getInitParameter("forwardContext"); 57 if( param != null ) 58 forwardContext = param; 59 } 60 61 72 protected void doGet(HttpServletRequest request, HttpServletResponse response) 73 throws ServletException , IOException 74 { 75 boolean trace = log.isTraceEnabled(); 76 if( trace ) 77 { 78 log.trace("["+forwardContext+"], PathInfo: "+request.getPathInfo() 79 + ", QueryString: "+request.getQueryString() 80 + ", ContextPath: "+request.getContextPath() 81 + ", HeaderNames: "+request.getHeaderNames() 82 + ", isCommitted: "+response.isCommitted() 83 ); 84 } 85 String path = request.getPathInfo(); 86 ServletContext sc = getServletContext().getContext(forwardContext); 87 if( sc != null ) 88 { 89 if( trace ) 90 log.trace("Found ServletContext for: "+forwardContext); 91 RequestDispatcher rd = sc.getRequestDispatcher(path); 92 if( rd != null ) 93 { 94 if( trace ) 95 log.trace("Found RequestDispatcher for: "+path); 96 rd.forward(request, response); 97 return; 98 } 99 } 100 throw new ServletException ("No RequestDispatcher for: "+forwardContext+"/"+path); 101 } 102 103 } 104 | Popular Tags |