Java 一个servlet如何从另一个servlet获取数据?

Java 一个servlet如何从另一个servlet获取数据?,java,servlets,response,Java,Servlets,Response,我的问题是这样的: 我有两个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://loca

我的问题是这样的:
我有两个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\u A从servlet\u B获取消息“helloworld”,然后将此消息响应到Firefox。

我得到如下答案:

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";
}

你应该试试短信服务。您想要的需求可以通过JMS实现。如果您想要的是双工通信,请尝试WebSocket。Boss不允许我使用JMS。最后,我休眠servlet_A,然后将“helloword”放入应用程序,然后通知servlet_A。