java在一个servlet中设置值,并从不同的servlet访问它
我有一个servletOne在本地TOMCAT中运行,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) 个答案