Java 远程服务器的RequestDispatcher?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3916214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:50:47  来源:igfitidea点击:

RequestDispatcher for remote server?

javaservlets

提问by bjornl

I am trying to create a HttpServlet that forwards all incoming requests as is, to another serlvet running on a different domain.

我正在尝试创建一个 HttpServlet 将所有传入请求按原样转发到另一个在不同域上运行的 serlvet。

How can this be accomplished? The RequestDispatcher's forward() only operates on the same server.

如何做到这一点?RequestDispatcher 的 forward() 仅在同一台服务器上运行。

Edit: I can't introduce any dependencies.

编辑:我不能引入任何依赖项。

采纳答案by BalusC

You can't when it doesn't run in the same ServletContextor same/clustered webserver wherein the webapps are configured to share the ServletContext(in case of Tomcat, check crossContextoption).

当它不在同一个ServletContext或同一个/集群的网络服务器中运行时,你不能,其中 webapps 被配置为共享ServletContext(在 Tomcat 的情况下,选中crossContext选项)。

You haveto send a redirect by HttpServletResponse.sendRedirect(). If your actualconcern is reusing the query parameters on the new URL, just resend them along.

必须通过 发送重定向HttpServletResponse.sendRedirect()。如果您真正关心的是在新 URL 上重用查询参数,只需重新发送它们。

response.sendRedirect(newURL + "?" + request.getQueryString());

Or when it's a POST, send a HTTP 307redirect, the client will reapply the same POST query parameters on the new URL.

或者当它是 POST 时,发送HTTP 307重定向,客户端将在新 URL 上重新应用相同的 POST 查询参数。

response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader("Location", newURL);


Updateas per the comments, that's apparently not an option as well since you want to hide the URL. In that case, you have to let the servlet play for proxy. You can do this with a HTTP client, e.g. the Java SE provided java.net.URLConnection(mini tutorial here) or the more convenienced Apache Commons HttpClient.

根据评论更新,这显然不是一个选项,因为您想隐藏 URL。在这种情况下,您必须让 servlet 为代理服务。您可以使用 HTTP 客户端执行此操作,例如提供的 Java SE java.net.URLConnection此处迷你教程)或更方便的Apache Commons HttpClient

If it's GET, just do:

如果是 GET,请执行以下操作:

InputStream input = new URL(newURL + "?" + request.getQueryString()).openStream();
OutputStream output = response.getOutputStream();
// Copy.

Or if it's POST:

或者如果是 POST:

URLConnection connection = new URL(newURL).openConnection();
connection.setDoOutput(true);
// Set and/or copy request headers here based on current request?

InputStream input1 = request.getInputStream();
OutputStream output1 = connection.getOutputStream();
// Copy.

InputStream input2 = connection.getInputStream();
OutputStream output2 = response.getOutputStream();
// Copy.

Note that you possibly need to capture/replace/update the relative links in the HTML response, if any. Jsoupmay be extremely helpful in this.

请注意,您可能需要捕获/替换/更新 HTML 响应中的相关链接(如果有)。Jsoup在这方面可能非常有帮助。

回答by Neeme Praks

Jetty has a sample ProxyServletimplementation that uses URL.openConnection()under the hood. Feel free to use as-is or to use as inspiration for your own implementation. ;-)

Jetty 有一个示例ProxyServlet实现,它在底层使用URL.openConnection()。随意使用原样或用作您自己实现的灵感。;-)

Or you can use Apache HttpClient, see the tutorial.

或者您可以使用 Apache HttpClient,请参阅教程

回答by Mike Baranczak

As others have pointed out, what you want is a proxy. Your options:

正如其他人指出的那样,您想要的是代理。您的选择:

  1. Find an open-source Java library that does this. There are a few out there, but I haven't used any of them, so I can't recommend any.

  2. Write it yourself. Shouldn't be too hard, just remember to deal with stuff like passing along all headers and response codes.

  3. Use the proxy module in Apache 2.2. This is the one I'd pick, because I already know that it works reliably.

  1. 找到一个开源 Java 库来执行此操作。那里有一些,但我没有使用过任何一个,所以我不能推荐任何一个。

  2. 自己写吧。不应该太难,只要记住处理诸如传递所有标头和响应代码之类的事情。

  3. 使用 Apache 2.2 中的代理模块。这是我会选择的一个,因为我已经知道它可以可靠地工作。