首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在应用程序之间发送参数

在应用程序之间发送参数
EN

Stack Overflow用户
提问于 2013-01-24 02:31:47
回答 2查看 154关注 0票数 1

是否有一种方法可以在同一台计算机上的应用程序之间发送属性?

例如:

代码语言:javascript
运行
复制
// 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);

代码语言:javascript
运行
复制
// IN APPLICATION 2'S (APP-2)  SERVLET

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

假设在其中部署app-1的IP与部署app-2的IP不一样。

有什么方法可以在彼此之间托管的应用程序之间发送这样的参数吗?当我尝试,我不能这样做,但可能有一个办法。

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

EN

回答 2

Stack Overflow用户

发布于 2013-01-24 02:48:08

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

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

来自Javadocs:

“警告您,当来自servlet的请求由RequestDispatcher驻留在不同的web应用程序中时,此方法设置的对象可能无法在调用者servlet中正确检索。”

票数 1
EN

Stack Overflow用户

发布于 2013-01-24 03:23:43

您可以创建要通过应用程序使用的基本包。

代码语言:javascript
运行
复制
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;
    }
}

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

代码语言:javascript
运行
复制
ArrayList<JSONObject> obj = new GetXMLTask().getOutputFromUrl1("url for the other application method which responds");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14492960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档