ServletContext.getRequestDispatcher()vs ServletRequest.getRequestDispatcher() [英] ServletContext.getRequestDispatcher() vs ServletRequest.getRequestDispatcher()
问题描述
为什么
getLquestDispatcher(字符串路径)
ServletRequest接口不能
延伸到当前servlet之外
context
getRequestDispatcher(String path) of the ServletRequest interface cannot extend outside the current servlet context
其中
getLquestDispatcher(String path)
ServletContext可以使用
getContext(String uripath)方法
获取资源$ RequestDispatcher b $ b在国外背景下。
getRequestDispatcher(String path) of the ServletContext can use the getContext(String uripath) method to obtain RequestDispatcher for resources in foreign contexts.
以及如何?
请帮助
推荐答案
如果使用绝对路径,例如(/ index.jsp
),没有区别。
If you use an absolute path such as ("/index.jsp"
), there is no difference.
如果使用相对路径,则必须使用 HttpServletRequest.getRequestDispatcher()
。 ServletContext.getRequestDispatcher()
不允许。
If you use relative path, you must use HttpServletRequest.getRequestDispatcher()
. ServletContext.getRequestDispatcher()
doesn't allow it.
例如,如果您在<$上收到请求c $ c> http://example.com/myapp/subdir ,
RequestDispatcher dispatcher =
request.getRequestDispatcher("index.jsp");
dispatcher.forward( request, response );
将请求转发到页面 http://example.com/ myapp / subdir / index.jsp
。
在任何情况下,您都不能将请求转发到上下文之外的资源。
In any case, you can't forward request to a resource outside of the context.
这篇关于ServletContext.getRequestDispatcher()vs ServletRequest.getRequestDispatcher()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!