call a servlet's post method from another application's servlet

我与影子孤独终老i 提交于 2020-01-06 19:41:10

问题


I want to call a servlet's POST method from another application, in which I am passing request and response. Can anyone tell me how is that possible?


回答1:


If your servlet is invoked in an HTTP POST then you can do an HTTP 307 redirect to another servlet and it will call it's doPost. If you want to POST to a different page from a servlet (or any Java method) you can compose a POST with something like HttpClient like this:

PostMethod post = new PostMethod("http://jakarata.apache.org/");
NameValuePair[] data = {
  new NameValuePair("user", "joe"),
  new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();


来源:https://stackoverflow.com/questions/12701769/call-a-servlets-post-method-from-another-applications-servlet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!