Java 跨应用程序发送参数
有没有一种方法可以跨可能在同一台机器上也可能不在同一台机器上的应用程序发送属性 例如:Java 跨应用程序发送参数,java,jakarta-ee,servlets,requestdispatcher,Java,Jakarta Ee,Servlets,Requestdispatcher,有没有一种方法可以跨可能在同一台机器上也可能不在同一台机器上的应用程序发送属性 例如: // 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
// 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);
让我们假设部署app-1的IP与部署app-2的IP不同 是否有任何方法可以跨彼此远离的应用程序发送此类参数?当我尝试时,我不能这样做,但可能有一种方法
这两个应用程序都使用Tomcat。如果要在数量可变的机器之间共享状态,那么使用HTTP作为存储该状态的方法不是很可靠 “属性”不是通过HTTP传输的,它们只是驻留在给定会话的应用程序上的共享状态。属性是100%纯服务器端信息 从Javadocs: “当从servlet发送请求时,会发出警告 由RequestDispatcher驻留在不同的web应用程序中 调用程序中可能无法正确检索此方法设置的对象 servlet。“
如果要在数量可变的机器之间共享状态,那么使用HTTP作为存储该状态的方法不是很可靠 “属性”不是通过HTTP传输的,它们只是驻留在给定会话的应用程序上的共享状态。属性是100%纯服务器端信息 从Javadocs: “当从servlet发送请求时,会发出警告 由RequestDispatcher驻留在不同的web应用程序中 调用程序中可能无法正确检索此方法设置的对象 servlet。“
您可以创建要通过应用程序使用的基本包
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;
}
}
包库;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类GetXMLTask
{
静态双纵;
静态双纬度;
公共ArrayList getOutputFromUrl1(字符串url)
{
ArrayList输出=新的ArrayList();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse响应;
StringBuilder=新的StringBuilder();
JSONObject myjson;
JSONArray_json_数组;
尝试
{
response=httpClient.execute(httpPost);
BufferedReader in=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent(),“UTF-8”);
char[]buf=新字符[8000];
int l=0;
而(l>=0)
{
附加(buf,0,l);
l=英寸读数(buf);
}
myjson=newJSONObject(“{child:+builder.toString()+”}”);
JSONObject mmm=新的JSONObject(builder.toString());
JSONArray mmmArr=mmm.getJSONArray(“状态”);
_json_数组=myjson.getJSONArray(“子”);
for(int i=0;i
现在,从您的应用程序调用此方法
ArrayList<JSONObject> obj = new GetXMLTask().getOutputFromUrl1("url for the other application method which responds");
ArrayList obj=new GetXMLTask().getOutputFromUrl1(“响应的其他应用程序方法的url”);
您可以创建一个要通过应用程序使用的基本包
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;
}
}
包库;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类GetXMLTask
{
静态双纵;
静态双纬度;
公共ArrayList getOutputFromUrl1(字符串url)
{
ArrayList输出=新的ArrayList();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse响应;
StringBuilder=新的StringBuilder();
JSONObject myjson;
JSONArray_json_数组;
尝试
{
response=httpClient.execute(httpPost);
BufferedReader in=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent(),“UTF-8”);
char[]buf=新字符[8000];
int l=0;
而(l>=0)
{
附加(buf,0,l);
l=英寸读数(buf);
}
myjson=newJSONObject(“{child:+builder.toString()+”}”);
JSONObject mmm=新的JSONObject(builder.toString());
JSONArray mmmArr=mmm.getJSONArray(“状态”);
_json_数组=myjson.getJSONArray(“子”);
for(int i=0;i