獲取RequestDispatcher有三中方法 web
1.RequestDispatcher dispatch = request.getRequestDispatcher(arg);這裏的參數能夠是一個相對路徑或者是一個不定的資源(sp, servlet) app
2.RequestDispatcher dispatch = getServletContext.getRequestDispatcher(arg)這裏的參數必須是一個絕對路徑RUL(已/開始),有效的servlet或者jsp.這裏的URL是在web.xml文件裏定義的,必須以/開始 jsp
3.RequestDispatcher dispatch = getServletContext.getNamedDispatcher("string"); 這裏的參數必須是一個servlet的名字.在web.xml定義 url
Example spa
1 NamedDispatcher code
Returns a RequestDispatcher object that acts as a wrapper for the named servlet. xml
getNamedDispatcher(String) method takes the name of the Servlet as parameter which is declared via Deployment descriptor. Example: Deployment Descriptor ip
<servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.example.ServletExample</servlet-class> </servlet>
RequestDispatcher dispatch = request.getNamedDispatcher(?FirstServlet?); dispatch.forward(request, response);2.
RequestDispatcher dispatch = request.getRequestDispatcher("/tes");
<servlet-mapping> <servlet-name>Test</servlet-name> <url-pattern>/tes</url-pattern> </servlet-mapping>3.
RequestDispatcher dispatch = request.getRequestDispatcher("/TestJspOne.jsp");