1

Is there a way I can send attributes across applications that may or may not be on the same machine ?

For example :

// 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

Let us suppose that IP on which app-1 is deployed is not the same as the IP on which the app-2 is deployed.

Is there any way I can send parameters like these across applications that are hosted far away from each other ? When I tried I couldn't do this way,but may be there is a way around.

Both the applications use Tomcat.

4

2 に答える 2

1

さまざまな数のマシン間で状態を共有する場合、その状態を格納する方法として HTTP を使用することはあまり信頼できません。

「属性」は HTTP 経由で送信されるのではなく、特定のセッションのアプリケーションに存在する共有状態にすぎません。属性は 100% 純粋にサーバー側の情報です。

Javadoc から:

「リクエストが RequestDispatcher によって別の Web アプリケーションに存在するサーブレットからディスパッチされると、このメソッドによって設定されたオブジェクトが呼び出し元のサーブレットで正しく取得されない可能性があることに注意してください。」

于 2013-01-24T02:48:08.933 に答える
0

アプリケーションで使用する基本パッケージを作成できます

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");
于 2013-01-24T03:23:43.200 に答える