从 servlet 调用 servlet
我想从另一个 servlet 调用一个 servlet,做两件事:
- 将内容类型设置为“multipart/form-data”,
- 将方法设置为“POST”。
从表单中这很容易做到,但我需要从另一个 servlet 中做到这一点。有什么想法吗?
I would like to call a servlet from another servlet doing two things:
- setting the content type to "multipart/form-data"
- setting the method to "POST".
This is very easy to do from a form, but I need to do it from another servlet. Any ideas how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 java.net.HttpUrlConnection 或 Apache HTTP 客户端向其他 servlet 发送 POST/GET 请求。您基本上将像浏览器一样调用另一个 servlet。
You can use
java.net.HttpUrlConnection
or maybe Apache HTTP client to send a POST/GET request to the other servlet. You will basically be invoking the other servlet the same way a browser would.听起来像请求 转发 或 include 就是您要寻找的内容。您实际执行的操作取决于您打算对目标 servlet 的输出执行的操作。你打算以某种方式显示它吗?或者你只是简单地丢弃它?在某些情况下,您可能需要在调用这些方法的方式上更加“有创意”(例如,创建您自己的请求/响应实例,或者包装当前的请求/响应,以便隔离状态更改)。
或者,为了简单起见,您可能只想打开一个到目标 servlet 的映射 URL 的网络连接,如 Jeff 建议。
It sounds like request forwarding or include is what you're looking for. What you actually do will depend on what you intend to do with the output of the target servlet. Are you going to display it somehow? Or are you simply discarding it? You may in some cases, need to be a bit more "creative" in how you invoke those methods (e.g., either creating your own request/response instances, or wrapping the current request/response so that state changes are isolated).
Alternatively, to keep things simple you may want to just open a network connection to your target servlet's mapped URL as Jeff suggested.
听起来您想使用 java 发送 HTTP POST。我建议使用 apache HttpClient。看看这个问题 向 Apache HttpPost 添加参数
您也可以使用纯 java 来执行此操作(HttpUrlConnection)[ http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html]。
It sounds like you want to send an HTTP POST with java. I would recommend using apache HttpClient. Check out this question Add parameters to Apache HttpPost
You can also do this with pure java with (HttpUrlConnection)[ http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html].