gpt4 book ai didi

java - ServletContext.getRequestDispatcher() 与 ServletRequest.getRequestDispatcher()

转载 作者:IT老高 更新时间:2023-10-28 20:33:18 24 4
gpt4 key购买 nike

为什么

getRequestDispatcher(String path) of the ServletRequest interface cannot extend outside the current servlet context

在哪里

getRequestDispatcher(String path) of the ServletContext can use the getContext(String uripath) method to obtain RequestDispatcher for resources in foreign contexts.

如何?

请帮忙

最佳答案

如果使用绝对路径,例如 ("/index.jsp"),则没有区别。

如果使用相对路径,则必须使用HttpServletRequest.getRequestDispatcher()ServletContext.getRequestDispatcher() 不允许。

例如,如果您在 http://example.com/myapp/subdir 上收到您的请求,

    RequestDispatcher dispatcher = 
request.getRequestDispatcher("index.jsp");
dispatcher.forward( request, response );

将请求转发到页面http://example.com/myapp/subdir/index.jsp

在任何情况下,您都不能将请求转发到上下文之外的资源。

关于java - ServletContext.getRequestDispatcher() 与 ServletRequest.getRequestDispatcher(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1411114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com