【问题标题】:sending parameters across applications跨应用程序发送参数
【发布时间】:2013-01-24 02:31:47
【问题描述】:

有没有一种方法可以在可能位于或可能不在同一台计算机上的应用程序之间发送属性?

例如:

// IN APPLICATION 1 (APP-1)

request.setAttribute("Truth","Ghazal is the food for the soul of separation");
RequestDispatcher rd = request.getRequestDispatcher("http://IP/App-2/servlet");
rd.forward(request,response);

// IN APPLICATION 2'S (APP-2)  SERVLET

String truth = request.getAttribute("Truth").toString();
// NOW USE THIS STRING

假设部署app-1的IP与部署app-2的IP不同。

有什么方法可以在彼此远离的应用程序之间发送此类参数?当我尝试时,我不能这样做,但可能有办法。

这两个应用程序都使用 Tomcat。

【问题讨论】:

  • 我认为您的问题的答案是网络服务
  • @HussainAkhtarWahid 你能详细说明一下吗
  • 创建一个响应其他应用程序的 Web 应用程序,或者如果您正在处理两个正在运行的应用程序,则创建一个 Web 服务以从一个应用程序获取价值并发送到另一个应用程序
  • @HussainAkhtarWahid 如果可以的话,为什么要使用第三个来连接两个应用程序?我们可以直接连接两个应用程序!
  • 是的,实际上我不是在说不同的模块,而是说应用程序发送和接收信息中的通用模块

标签: java jakarta-ee servlets requestdispatcher


【解决方案1】:

如果您要在可变数量的机器之间共享状态,那么使用 HTTP 作为存储该状态的方法不是很可靠。

“属性”不通过 HTTP 传输,它们只是驻留在给定会话的应用程序上的共享状态。属性是 100% 纯服务器端信息。

来自 Javadocs:

"从servlet派发请求时发出警告 RequestDispatcher 驻留在不同的 Web 应用程序中, 此方法设置的对象可能无法在调用者中正确检索 servlet。”

【讨论】:

  • 那么您能否描述一下我如何跨应用程序发送属性?
【解决方案2】:

您可以创建一个基础包以通过应用程序使用

package base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class GetXMLTask
{
    static double longitute;
    static double latitude;
    public ArrayList<JSONObject> getOutputFromUrl1(String url) 
    {
        ArrayList<JSONObject> output = new ArrayList<JSONObject>();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response;
        StringBuilder builder= new StringBuilder();
        JSONObject myjson ;
        JSONArray the_json_array;
        try 
        {
            response = httpClient.execute(httpPost);
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            char[] buf = new char[8000];
            int l = 0;
                while (l >= 0) 
                {
                    builder.append(buf, 0, l);
                    l = in.read(buf);
                }
            myjson = new JSONObject("{child:"+builder.toString()+"}");
            JSONObject mmm = new JSONObject(builder.toString());
            JSONArray mmmArr = mmm.getJSONArray("status");
            the_json_array = myjson.getJSONArray("child");
            for (int i = 0; i < the_json_array.length(); i++) 
            {
                JSONObject another_json_object =  the_json_array.getJSONObject(i);
            output.add(another_json_object);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return output;
    }
}

现在从您的应用程序调用此方法

ArrayList<JSONObject> obj = new GetXMLTask().getOutputFromUrl1("url for the other application method which responds");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 2013-03-09
    • 2011-11-20
    • 2021-10-06
    • 2015-11-16
    • 2011-12-07
    • 1970-01-01
    相关资源
    最近更新 更多