Home > Enterprise >  Jetty Request attributes not propagated across services
Jetty Request attributes not propagated across services

Time:04-24

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
}
}

CodePudding user response:

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?)

  • Related