Bhu*_*han 3 java tomcat web-services servlets rmi
我有两个Web应用程序,webAppMaster和webAppSlave,部署在同一个tomcat服务器中.现在在webAppMaster应用程序中,有一个java类RequestHandler,其processRequest方法将customObject1作为参数并返回customObject2.现在,从RequestCreator类的webAppSlave应用,我想调用的processRequest的方法RequestHandler类的webAppMaster应用.应该怎么做?提前致谢.
您需要在应用程序之间进行交谈,就像在两个远程应用程序之间进行通话 它们在同一台服务器上并不重要,它们必须使用某种协议进行通信.
你想要做的实际上是RMI(远程方法调用) - http://docs.oracle.com/javase/tutorial/rmi/
而不是使用rmi,您可以使用更轻量级的通信方式.例如,您可以通过Rest进行通信.在这种情况下,在webAppMaster应用程序中创建servlet,它将customObject1序列化为JSON作为参数(作为URL请求参数或使用POST方法).比这个servlet将JSON字符串转换为customObject1并调用processRequest.稍后在processRequest()返回customObject2后将其转换为JSON并发送回客户端.在客户端读取json并将JSON反序列化回webappSlave中的customObject2.
public class MasterServlet extends javax.servlet.http.HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CustomObject1 customObject1 = buildCustomObject1BasingOnRequestParams(HttpServletRequest request); // read the request params and build your object either from json or whatever format webappSlave used to send
CustomObject2 customObject2 = RequestHandler.processRequest(customObject1);
String json = transformTOJson(customObject2); // there are many libaries which does this
response.getWriter().print(json);
}
}
Run Code Online (Sandbox Code Playgroud)
你的奴隶应用程序会做相反的事情.首先将customObject1序列化为JSON,然后将收到的JSON反序列化为customObjec2.
作为第三个选项,您可以使用HTTP隧道在应用程序之间发送对象(例如,请参阅此帖子:通过HTTP序列化以正确的方式转换对象.)作为示例.
| 归档时间: |
|
| 查看次数: |
3674 次 |
| 最近记录: |