2011-11-02 117 views
0

我有兩個WAR應用程序,它們之間的通信模式是通過servlet。我的應用程序(WAR A)用另一個WAR中的servlet的URL打開一個子窗口(讓我們說WAR B)。RequestDispatcher以無限循環結尾

servlet(在WAR B中)處理數據並應將處理後的數據發送回原始應用程序的servlet(即WAR A的servlet)。

但是,此過程以無限循環結束,並且WAR-A發送的URL參數也爲空。

這裏是代碼片段:

下面的腳本將打開的servlet的戰爭-B還通過一些URL參數的URL子窗口。

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

下面是WAR-B,提取的URL參數和處理數據,然後重新發送請求回WAR-A的Servlet 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); 
    } 
} 

有誰有想法這個?

問候,

薩蒂亞

回答

1

這則只是意味着該servlet基本上自稱每次。我目前沒有立即看到給出的信息中的原因,但顯然您傳遞到getRequestDispatcher()的URL與servlet本身的URL匹配。

然而,我在這裏看到的一個重大失誤:

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

,可以調用不可能運行在另一個servlet上下文中的servlet(讀:另一場戰爭)。首先需要ServletContext#getContext()才能獲取其他servlet上下文,然後使用ServletContext#getRequestDispatcher()將請求發送到那裏。

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

這隻需要兩個WAR都配置爲公開上下文以便共享。例如,在Tomcat上,這可以通過將crossContext="true"添加到<Context>來完成。

+0

我定義web.xml的不好的風格。 web.xml的url-pattern爲'/ *',因此servlet被重複調用導致StackOverfow :) – Satya