HI I like to know there is a single method called getRequestDispatcher() in ServletRequest and ServletContext interfaces. What is the difference?
views:
144answers:
2As 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.