码头请求属性未在服务中传播

发布于 01-23 12:57 字数 637 浏览 2 评论 0 原文

服务A向前请求服务2使用但是,在请求A到达服务B时,​​请求A中设置的属性A不可用。Servlet请求属性是否在HTTP调用中不持续不存在?有人可以帮助我了解发生了什么事吗?

public class ForwardServlet extends AsyncProxyServlet
{
  //Service A
...
@Override
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
...
request.setAttribute("FOO", "BAR"); // This attribute is missing in Service B
    super.service(request, response);// Send to service B
}
}

Service A forwards requests to Service 2 using AsyncProxyServlet but the attributes set in the request by service A is not available in the request once it reaches Service B. Are servlet request attributes not persistent across HTTP calls? Could someone please help me in understanding whats going on?

public class ForwardServlet extends AsyncProxyServlet
{
  //Service A
...
@Override
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
...
request.setAttribute("FOO", "BAR"); // This attribute is missing in Service B
    super.service(request, response);// Send to service B
}
}

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

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

发布评论

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

评论(1

寂寞清仓 2025-01-30 12:57:33

Servlet请求属性是Servlet规范的一部分,是一种在同一Servlet容器内单个调度链上保持值的方法。

这些值并非作为任何HTTP协议规格的一部分存在,也不能通过新的HTTP请求发送。

用servlet术语进行调度意味着...

  1. 已解析了传入的HTTP请求
  2. httpservletrequest 对象已创建了
  3. 对目标 servlet 的调用链,已从组合中确定过滤 servlet url-patterns
  4. 在链条中,
  5. 每个过滤器和最终目的地 都发生了第一个 filter servlet可以添加/更改每个过滤器或servlet的请求属性
  6. ,也可以将 requestDisPatcher 添加到 .forward(req,resp))或 .include(req,resp) 同一派遣到同一servlet容器上的新位置。

如果您的服务B位于同一Servlet容器上,请使用 requestDisPatcher ,您的请求属性将出现在乘车。

如果您的服务B位于其他服务器上,则您需要弄清楚如何使用HTTP协议(也许是请求标头?)或您的请求实体内容(作为JSON值? )

Servlet Request Attributes are part of the Servlet spec, and are a way to hold onto values across a single dispatch chain within the same Servlet container.

These values do not exist as part of any HTTP protocol spec, and cannot be sent via new HTTP requests.

A dispatch in servlet terms means ...

  1. the incoming HTTP Request has been parsed
  2. an HttpServletRequest object has been created
  3. the chain of calls to the destination Servlet has been determined from the combination of Filter and Servlet url-patterns
  4. the dispatch occurs to the first Filter in the chain
  5. each filter and eventual destination servlet can add/change the request attributes
  6. each filter or servlet can also obtain a RequestDispatcher to .forward(req, resp) or .include(req, resp) the same dispatch to a new location on the same Servlet container.

If your service B is on the same Servlet container, use RequestDispatcher and your Request attributes will come along for the ride.

If your service B is on a different server, then you'll need to figure out how to transfer those attributes to the destination server using either the HTTP protocol (maybe as request headers?) or within your request body content (as JSON values?)

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