single method in getRequestDispatcher() in ServletRequest and ServletContext interfaces
HI I like to know there is a single method called getRequestDispatcher() in ServletRequest an开发者_Go百科d ServletContext interfaces. What is the difference?
As stated in the Servlet API Javadocs,
The difference between this method [the ServletRequest one] and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.
You can pass a relative path to getRequestDispatcher() of ServletRequest but not to getRequestDispatcher() of ServletContext.
Example:
My current request is served from page - webapp/view/core/bar.jsp and requested page - webapp/view/util/foo.jsp
request.getRequestDispatcher("../util/foo.jsp") is valid and will be evaluated to the path relative to current request.
servletContext.getRequestDispatcher("/view/util/foo.jsp") is valid and will evaluate from context root.
This is because ServletContext will not be aware of current request path. If you decide to use '/' root to access your resources, then both ways are same.
精彩评论