2016-11-22 476 views
-1

我的問題是這樣的:
我有兩個servlet:
servlet_A:http://localhost:8080/test/servlet_wait_for_response
servlet_B:http://localhost:8080/test/servlet_send_data?data=xxx
1.我使用Firefox調用servlet_A和servlet_A什麼也不做,但等待;
2.我使用Chrome調用servlet_B,併發送「helloworld」給服務器,例如:http://localhost:8080/test/servlet_send_data?data=helloworld
3. servlet_B獲取消息「helloworld」,然後將此消息發送給servlet_A;
4. servlet_A從servlet_B獲取消息「helloworld」,然後將此消息回覆給Firefox。
servlet如何從另一個servlet獲取數據?

+0

你應該嘗試的消息服務。 JMS可以滿足您的要求。如果你想要的是雙工通信,請嘗試WebSockets。 – 9ine

+0

老闆不允許我使用JMS。最後,我睡了servlet_A,然後把'helloword'放到應用程序中,然後通知servlet_A。 – light

回答

0

我得到的回答象下面這樣:

static String msg = null; 

@RequestMapping(value = "/wait_for_data", method = RequestMethod.GET) 
@ResponseBody 
public String waitData() throws InterruptedException{ 
    while(msg==null){ 
     TimeUnit.SECONDS.sleep(1); 
    } 
    return msg; 
} 

@RequestMapping(value = "/send_data", method = RequestMethod.GET) 
@ResponseBody 
public String sendData(String data){ 
    msg = data; 
    return "OK"; 
} 
相關問題