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.

saplingPro
  • 20,769
  • 53
  • 137
  • 195

2 Answers2

1

If you are going to be sharing state between a variable number of machines, then using HTTP as the method to store that state is not very reliable.

"Attributes" are not transmitted over HTTP, they are merely shared state that reside on the application for the given session. Attributes are 100% purely server-side information.

From the Javadocs:

"It is warned that when the request is dispatched from the servlet resides in a different web application by RequestDispatcher, the object set by this method may not be correctly retrieved in the caller servlet."

stan
  • 4,885
  • 5
  • 49
  • 72
0

you can create a base package to be used through the application

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

now from your application call this method by

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