我有两个WAR应用程序,它们之间的通信方式是通过servlet。
我的应用程序(WAR A)打开一个子窗口,其中包含另一个WAR中的servlet的URL(假设为WAR B)。
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);
}
}
有人对此有所了解吗?
此致
萨蒂亚
答案 0 :(得分:1)
那就意味着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>
来完成此操作。