0

我有两个 WAR 应用程序,它们之间的通信方式是通过 servlet。

我的应用程序(WAR A)打开一个子窗口,其中包含另一个 WAR(假设为 WAR B)中的 servlet 的 URL。

servlet(在 WAR B 中)处理数据并将处理后的数据发送回原始应用程序的 servlet(即 WAR A 的 servlet)。

但是这个过程以无限循环结束,并且从 WAR-A 发送的 URL 参数也为空。

这是代码片段:

下面的脚本打开一个子窗口,其中包含 WAR-B 中的 servlet 的 URL,也传递了一些 URL 参数。

function invokePlugin(invokeURL, custValJSON, meaCompPartJSON) {
    window.open(invokeURL + '?custValJSON=' + custValJSON,'');
}

下面是 WAR-B 中的 servlet 代码,它提取 URL 参数并处理数据,然后再次将请求发送回 WAR-A 的 servlet...

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String custValJSON = request.getParameter("custValJSON");
    System.out.println("custValJSON : " + custValJSON);

    CustomValues custVal = gson.fromJson(custValJSON, CustomValues.class);

    if(custVal != null) {
        System.out.println("Cust val details : " + custVal.getName());
        custVal.setValue("Satya");
    }

    String destination = "/testPlannerPluginResult";

    RequestDispatcher reqDispatch = request.getRequestDispatcher(destination);
    request.setAttribute("custValJSON", gson.toJson(custVal));

    if(reqDispatch != null) {
        reqDispatch.forward(request, response);
    }
}

有人对此有想法吗?

问候,

萨提亚

4

1 回答 1

2

那只是意味着 servlet 基本上每次都在调用自己。到目前为止,我没有立即在给出的信息中看到原因,但显然您传入getRequestDispatcher()的 URL 与 servlet 本身的 URL 相匹配。

然而,我在这里看到了一个重大错误:

RequestDispatcher reqDispatch = request.getRequestDispatcher(destination);
request.setAttribute("custValJSON", gson.toJson(custVal));

这不可能调用在另一个 servlet 上下文中运行的 servlet(阅读:另一个 WAR)。您需要ServletContext#getContext()首先获取另一个 servlet 上下文,然后使用ServletContext#getRequestDispatcher()将请求分派到那里。

ServletContext otherServletContext = getServletContext().getContext("/otherContextPath");
RequestDispatcher dispatcher = otherServletContext.getRequestDispatcher(destination);

这只需要将两个 WAR 都配置为公开上下文以供共享。例如,在 Tomcat 上,这是通过crossContext="true"<Context>.

于 2011-11-02T13:31:25.630 回答