1 16 package org.mortbay.servlet; 17 import java.io.IOException ; 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import javax.servlet.RequestDispatcher ; 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServlet ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.commons.logging.Log; 31 import org.mortbay.log.LogFactory; 32 33 34 45 public class Forward extends HttpServlet 46 { 47 private static Log log = LogFactory.getLog(Forward.class); 48 49 50 Map _forwardMap= new HashMap (); 51 52 53 public void init(ServletConfig config) 54 throws ServletException 55 { 56 super.init(config); 57 58 Enumeration enm = config.getInitParameterNames(); 59 while (enm.hasMoreElements()) 60 { 61 String path=(String )enm.nextElement(); 62 String forward=config.getInitParameter(path); 63 _forwardMap.put(path,forward); 64 } 65 66 } 67 68 69 public void doPost(HttpServletRequest sreq, HttpServletResponse sres) 70 throws ServletException , IOException 71 { 72 doGet(sreq,sres); 73 } 74 75 76 public void doGet(HttpServletRequest sreq, HttpServletResponse sres) 77 throws ServletException , IOException 78 { 79 String path = (String ) 80 sreq.getAttribute("javax.servlet.include.servlet_path"); 81 if (path==null) 82 path=sreq.getServletPath(); 83 if (path.length()==0) 84 { 85 path=(String )sreq.getAttribute("javax.servlet.include.path_info"); 86 if (path==null) 87 path=sreq.getPathInfo(); 88 } 89 90 String forward=(String )_forwardMap.get(path); 91 if(log.isDebugEnabled())log.debug("Forward "+path+" to "+forward); 92 if (forward!=null) 93 { 94 ServletContext context = 95 getServletContext().getContext(forward); 96 String contextPath=sreq.getContextPath(); 97 if (contextPath.length()>1) 98 forward=forward.substring(contextPath.length()); 99 100 RequestDispatcher dispatch = 101 context.getRequestDispatcher(forward); 102 if (dispatch!=null) 103 { 104 dispatch.forward(sreq,sres); 105 return; 106 } 107 } 108 109 sres.sendError(404); 110 } 111 112 113 public String getServletInfo() 114 { 115 return "Forward Servlet"; 116 } 117 118 119 public synchronized void destroy() 120 { 121 log.debug("Destroyed"); 122 } 123 124 } 125 | Popular Tags |