使Java servlet充当代理的代码? [英] Code to get a Java servlet to act as a proxy?

查看:142
本文介绍了使Java servlet充当代理的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Java Web应用程序,它们有一个servlet,它被映射到一个特定的URL:

I have two Java web applications that have a single servlet that gets mapped to a specific URL:

red.war/
    WEB-INF/classes
        com.me.myorg.red.RedServlet (maps to http://red.example.com/doStuff)
blue.war/
    WEB-INF/classes
        com.me.myorg.blue.BlueServlet (maps to http://blue.example.com/doStuff)

我想把这些应用程序(我称之为后端应用程序)放在代理应用程序(servlet)后面,这将决定这两个应用程序中的哪一个最终将服务于客户端请求。

I want to put these application (I'm calling them my "backend apps") behind a "proxy app" (servlet) that will decide which of these two apps will ultimately service a client-side request.

此代理Web应用程序将接收传入的HTTP请求,并确定转发请求的2个后端应用程序(红色或蓝色)中的哪一个到。然后该请求将被转发到 http://red.example.com/doStuff (然后由 RedServlet#doGet(.. 。))或 http://blue.example.com/doStuff (然后由 BlueServlet#doGet处理( ...))。来自后端应用程序的返回响应(同样, RedServlet #doGet(...) BlueServlet #doGet(...))然后将返回到代理servlet,并最终返回给客户端。

This proxy web app would take an incoming HTTP request, and determines which of the 2 "backend apps" (red or blue) to forward the request onto. The request would then be forwarded on to either http://red.example.com/doStuff (and then processed by RedServlet#doGet(...)) or http://blue.example.com/doStuff (and then processed by BlueServlet#doGet(...)). The returned response from the backend app (again, either RedServlet#doGet(...) or BlueServlet#doGet(...)) would then be returned to the proxy servlet, and ultimately returned to the client.

换句话说,在伪代码中:

In other words, in pseudo-code:

public class ProxyServlet extends HttpServlet {
    @Override
    public doGet(HttpServletRequest request, HttpServletResponse response) {
        String forwardingAddress;
        if(shouldBeRed(request))
            forwardingAddress = "http://red.example.com/doStuff";
        else
            forwardingAddress = "http://blue.example.com/doStuff";

        PrintWriter writer = response.getWriter();

        writer.write(getResponseFromBackend(forwardingAddress, request));
    }

    private String getResponseFromBackend(String addr, HttpServletRequest req) {
        // Somehow forward req to addr and get HTML response...
    }
}

这可能吗?如果是,那么代码如何以及代码是什么我需要编写才能使其正常工作?

Is this possible? If so, how and what code would I need to write to make it work?

推荐答案

您可以使用 RequestDispatcher 以下列方式转发您的请求:

You could use a RequestDispatcher to forward your request in the following way:

RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(forwardingAddress);

// here you have the choice whether to use include(..) or forward(..) see below
if(useInclude)
    dispatcher.include(httpRequest, httpResponse);
else
    dispatcher.forward(httpRequest, httpResponse);

...其中 useInlcude 设置为您可以选择以下选项:

... where useInlcude is set to your choice with the following options:


  • include
    这可能是您想要做的:加载来自 forwardingAdress 的内容到您的回复中。


    • 这意味着您甚至可以将多个目标包含在一个响应中。

    • 客户端甚至不会实现此过程他也不需要能够看到目标文件。

    • include
      This is probably what you want to do: Load the content from the forwardingAdress into your response.
      • This means you could even include multiple targets into a single response.
      • The client will not even realize this procedure nor does he need to be able to see the target document.

      • 如果你在带有开发者工具的浏览器中这样做,你会看到第二个请求。

      • 客户必须能够查看并加载目标网址。

      • 您只能转发到单个目标。

      请参阅以下链接:


      • RequestDispatcher javadoc ,特别是对于笔记:


        • 转发应该在响应提交给客户端之前调用(在响应之前)身体输出已被冲洗)。如果响应已经提交,则此方法抛出IllegalStateException。响应缓冲区中的未提交输出在转发前自动清除。

        • include :请求和响应参数必须与传递给调用的对象相同servlet的服务方法或者是包装它们的ServletRequestWrapper或ServletResponseWrapper类的子类。

        • RequestDispatcher javadoc, especially for the notes:
          • forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
          • include: The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.

          这篇关于使Java servlet充当代理的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆