ServletRequest.getRequestDispatcher() and ServletContext.getRequestDispatcher()

The difference between ServletRequest.getRequestDispatcher(String path) and ServletContext.getRequestDispatcher(String path) is that the former can accept a relative path as well whereas the latter can accept paths relative to the current context root only.

If the path starts with a ‘/’ in the getRequestDispatcher(String path) of the ServletRequest interface then it’s interpreted as being relative to the current context root otherwise it’ll be a relative to the request of the calling servlet. Whereas, the path of the getRequestDispatcher(String path) of the ServletContext interface must start with ‘/’ only – being relative to the current context root.

Another difference between the two is that path of the getRequestDispatche(String path) of the ServletRequest interface cannot extend outside the current servlet context whereas getRequestDispatcher(String path) of the ServletContext can use the getContext(String uripath) method to obtain RequestDispatcher for resources in foreign contexts.

你可能感兴趣的:(servletContext)