2016-11-22 54 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"; 
} 
相关问题