'Servlet_Jsp'에 해당되는 글 9건

  1. 2012.09.25 request.getRequestDispatcher() vs getServletContext().getRequestDispatcher() 1
2012. 9. 25. 12:26

request.getRequestDispatcher() vs getServletContext().getRequestDispatcher()

request.getRequestDispatcher() vs getServletContext().getRequestDispatcher()


Is there a different between both RequestDispatcher?

There is a small difference: 

From ServletRequest 
  • method to call: getRequestDispatcher(String path)
  • the path parameter doesn't have to start with a "/"
  • without a "/" it is relative to directory containing the Servlet. It doesn't have to exist as a real directory (because of the logical fiction of the servlet mapping)
  • cannot surpass the web-application


  • From ServletContext 
  • method to call: getRequestDispatcher(String path)
  • method to call: getNamedDispatcher(String name) - name should match a servlet-name
  • A servlet (identified with a servlet-name) without a servlet-mapping in the web.xml can be reached with the getNamedDispatcher()
  • the path parameter has to start with a "/", otherwise IllegalStateException
  • can surpass a web-application in the same server (JVM), using getContext() to obtain a foreign context. (note: Tomcat setting required in the servlet.xml: <Context crossContext="true"> )