【问题标题】:RequestDispatcher to transfer request to another web app on same serverRequestDispatcher 将请求传输到同一服务器上的另一个 Web 应用程序
【发布时间】:2012-03-17 05:46:01
【问题描述】:

是否可以通过请求调度程序将 http 回发请求发送到不同项目中的 servlet。我试过了,但是

给定的 url 在同一个项目中被检查并给出 404 错误...

HTTP Status 404 - /FilterI/http:/IP:8080/FilterII/RequestServlet

type Status report

message /FilterI/http:/IP:8080/FilterII/RequestServlet

description The requested resource (/FilterI/http:/10.49.11.197:8080/FilterII/RequestServlet) is not available.

两个战争都部署在同一台服务器上。我可以通过使用 .sendRedirect() 函数实现同样的事情。但是为什么它没有在 requestdispatcher 的帮助下运行。

【问题讨论】:

    标签: jsp servlets


    【解决方案1】:

    是的,使用ServletContext API。您可以使用 ServletContext.getContext("othercontext") 获取其他应用程序的 servlet 上下文,并在该应用程序中获取 servlet 的请求调度程序。

    下面是解释如何做到这一点的代码:

    ServletContext otherCtx  = currentServletContext.getContext("otherContext");
    RequestDispatcher dispatcher = otherCtx.getRequestDispatcher("/forwardedPath");
    dispatcher.forward(request, response);
    

    【讨论】:

    • 您忘了提及一个并非不重要的事实以及如何配置它:在大多数(如果不是所有)自尊容器上,获取其他 webapp 上下文的能力默认禁用 .
    • 在 tomcat 中,例如需要将 crossContext="true" 放入 context.xml 。 stackoverflow.com/a/6555794/21499
    【解决方案2】:

    The javadoc 说:

    指定的路径名​​可能是相对的,虽然 它不能扩展 在当前 servlet 上下文之外

    (强调我的)

    所以,你问的是不可能的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 2019-08-08
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多