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.
本文对比了ServletRequest与ServletContext接口中getRequestDispatcher方法的区别。前者可以接受相对路径,而后者仅能接受相对于当前上下文根的绝对路径。此外,还介绍了如何通过ServletContext的方法访问其他上下文中的资源。
1103

被折叠的 条评论
为什么被折叠?



