我有一个在本地TOMCAT中运行的servletOne,而servletTwo部署在远程位置的不同TOMCAT中。我想访问在servletTwo中设置的值(Array / ArrayList)。我不知道该怎么做。我看过不同的例子,但没有什么工作。这就是我到目前为止所拥有的。这是我的doPost
doGet(req, resp);
//doGet
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
ArrayList al = new ArrayList();
al.add("valOne");
al.add("valTwo");
//Should I use this
PrintWriter out = resp.getWriter();
out.println(al);
//Or should I use this
req.setAttribute("ArrayList", al);
}
现在我想从servletOne
访问arraylist在servletOne中我有,
try
{
URL url = new URL("http://myhost:8080/servletTest");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response;
while ((response = in.readLine()) != null)
{
System.out.println(response);
}
in.close();
}
catch (MalformedURLException ex)
{
// handle this exception
}
catch (IOException ex)
{
// handle this exception
}
}
我认为它不会起作用,因为我从字符串中获取值而不是servletOne中的数组。任何帮助将不胜感激。 提前谢谢。
答案 0 :(得分:0)
您可以使用JSON:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
ArrayList al = new ArrayList();
al.add("valOne");
al.add("valTwo");
//Should I use this
PrintWriter out = resp.getWriter();
out.println(al);
//Or should I use this
// req.setAttribute("ArrayList", al);
//create al object to JSOn and send it another servlet on another tomcat
Gson gson=new Gson();
String jsonlist=gson.toJson(al)
String url = "http://example.com/query?q=" + URLEncoder.encode(jsonlist, "UTF-8");
}