将发布数据从一个java servlet写入另一个 [英] Writing post data from one java servlet to another
本文介绍了将发布数据从一个java servlet写入另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个servlet,它将通过POST将XML文件(xml格式化的字符串)发送到另一个servlet。
(非必要的xml生成代码替换为Hello there)
I am trying to write a servlet that will send a XML file (xml formatted string) to another servlet via a POST. (Non essential xml generating code replaced with "Hello there")
StringBuilder sb= new StringBuilder();
sb.append("Hello there");
URL url = new URL("theservlet's URL");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Length", "" + sb.length());
OutputStreamWriter outputWriter = new OutputStreamWriter(connection.getOutputStream());
outputWriter.write(sb.toString());
outputWriter.flush();
outputWriter.close();
这导致服务器错误,并且永远不会调用第二个servlet。
This is causing a server error, and the second servlet is never invoked.
推荐答案
使用像 HttpClient 。甚至还有发布XML代码示例:
PostMethod post = new PostMethod(url);
RequestEntity entity = new FileRequestEntity(inputFile, "text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
这篇关于将发布数据从一个java servlet写入另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文