问题描述
为什么
getRequestDispatcher(String path) ofServletRequest 接口不能扩展到当前 servlet 之外上下文
哪里
getRequestDispatcher(String path) ofServletContext 可以使用getContext(String uripath) 方法获取资源的RequestDispatcher在外国语境中.
以及如何?
请帮忙
推荐答案
如果使用绝对路径如 ("/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.
例如,如果您在 http://example.com/myapp/subdir
上收到您的请求,
For example, if you receive your request on 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() 与 ServletRequest.getRequestDispatcher()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!