当前位置: 技术问答>java相关
在Servlet下,如何向另外的一个URL post一个大文本串?一定送分
来源: 互联网 发布时间:2015-01-04
本文导语: 我使用sendRedirect,只能使用get,无法传递一个大文本,而且我不想使用Session来存储这个文本。我想Servlet或Java的API一定可以解决的,诸位请帮忙 | String theCGI = "http://www.server.com/cgi-bin/aCGI.pl"; ...
我使用sendRedirect,只能使用get,无法传递一个大文本,而且我不想使用Session来存储这个文本。我想Servlet或Java的API一定可以解决的,诸位请帮忙
|
String theCGI = "http://www.server.com/cgi-bin/aCGI.pl";
String encoded = "XML=" + URLencoder.encode("YOUR_XML");
URL CGIurl = new URL(/tech-qa-java/theCGI/index.html);
URLConnection c = CGIurl.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);
c.setRequestProperty("content-type","application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes(encoded);
out.flush(); out.close();
BufferedReader in =
new BufferedReader(new InputStreamReader(c.getInputStream());
String aLine;
while ((aLine = in.readLine()) != null) {
// data from the CGI
System.out.println(aLine);
}
String encoded = "XML=" + URLencoder.encode("YOUR_XML");
URL CGIurl = new URL(/tech-qa-java/theCGI/index.html);
URLConnection c = CGIurl.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);
c.setRequestProperty("content-type","application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes(encoded);
out.flush(); out.close();
BufferedReader in =
new BufferedReader(new InputStreamReader(c.getInputStream());
String aLine;
while ((aLine = in.readLine()) != null) {
// data from the CGI
System.out.println(aLine);
}
|
为什么不用session,
或者用request.setAttribute("name",value);
在另一个servlet里用request.getAttribute("name");
或者用request.setAttribute("name",value);
在另一个servlet里用request.getAttribute("name");