从 servlet 调用外部 Web 服务

发布于 2024-09-02 08:15:54 字数 505 浏览 6 评论 0原文

我正在开发一个 servlet,它获取 Web 服务的名称,并且可以将请求转发到外部 Web 服务,例如:http://www.webservice.com/...

I我已经构建了一个响应包装器来拦截响应输出,但我无法将请求转发到外部 Web 服务,只有当我将请求重定向到同一服务器上的 servlet 时它才有效。

示例:

request.getRequestDispatcher("aMyServlet").forward(request, response) // WORKS
 request.getRequestDispatcher("http://www.webservice.com/...").forward(request, response)

不会,因为 Tomcat 在服务器上将 http://www.webservice.com/... 搜索为本地资源。

我该如何进行外部请求?

谢谢

I'm developing a servlet that gets a name of a web service and could be forward the request to an external web service, for example: http://www.webservice.com/...

I have build a response wrapper that intercept response output but I can't forward request to an external web service, it works only if I redirect the request to a servlet that is on same server.

Example:

request.getRequestDispatcher("aMyServlet").forward(request, response) // WORKS
 request.getRequestDispatcher("http://www.webservice.com/...").forward(request, response)

Doesn't because Tomcat searches http://www.webservice.com/... on the server as a local resource.

How can I do external request?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窗影残 2024-09-09 08:15:54

您正在使用的 forward 方法用于在服务器资源之间进行通信(例如:您已经发现的 servlet 到 servlet)如果您想重定向到另一个位置,您可以使用 HttpServletResponse 的sendRedirect 方法。
更好的选择是
执行您自己的 HTTP 请求并将结果流式传输回
浏览器。这听起来比做起来难。基本上你创建了一个
java.net.HttpURLConnection 替换为您想要访问的网站的 URL
“重定向”到。这实际上可以包含查询参数(只要
它们不太大),因为它永远不会发送到用户的浏览器
两者都不会出现在浏览器 URL 栏中。打开连接,得到
内容并将其写入 Servlet 的 OutputStream。

forward method that you are using is used for communicating between server resources, (eg: servlet to servlet as you have found out) If you want to redirect to another location, you can use the HttpServletResponse's sendRedirect method.
Better option is to
Perform your own HTTP request and stream the results back to the
browser. This sounds harder than it is. Basically you create a
java.net.HttpURLConnection with the URL of the web site you want to
"redirect" to. This can actually contain the query parameters (as long as
they're not too large) since it will never be sent to the user's browser
either and will not appear in the browser URL bar. Open the connection, get
the content and write it to the Servlet's OutputStream.

在巴黎塔顶看东京樱花 2024-09-09 08:15:54

要向外部服务发出任何请求,您必须显式发出新的 HTTP 请求并处理其响应。看一下 HttpUrlConnection 类。

To make any request to an outside service, you'll have to explicitly make a new HTTP request and handle its response. Take a look at the HttpUrlConnection class.

秋风の叶未落 2024-09-09 08:15:54

您没有提及要调用哪种类型的服务,但无论哪种方式,您的 servlet 都充当服务客户端,因此您应该查看服务客户端技术。

要调用 REST 样式服务,java.net.URLApache Commons HttpClient 可用于发送 URL 请求并获取响应。

要调用 SOAP 服务,您可以使用 Apache AxisJava WSIT

You don't mention what kind of service you want to invoke, but either way, your servlet is functioning as a service client, so you should be looking at service client technologies.

For invoking REST style services, java.net.URL or Apache Commons HttpClient can be used to send a request for a URL and fetch the reponse.

For invoking SOAP services, you can use Apache Axis, or Java WSIT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文