问题
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