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
}
}
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 ...
HttpServletRequest
object has been createdServlet
has been determined from the combination of Filter
and Servlet
url-patterns
Filter
in the chainRequestDispatcher
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?)