服务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
}
}
Servlet请求属性是Servlet规范的一部分,是一种在同一Servlet容器内单个调度链上保持值的方法。
这些值并非作为任何HTTP协议规格的一部分存在,也不能通过新的HTTP请求发送。
用servlet术语进行调度意味着...
httpservletrequest
对象已创建了servlet
的调用链,已从组合中确定过滤
和servlet
url-patterns
filter
servlet可以添加/更改每个过滤器或servlet的请求属性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 ...
HttpServletRequest
object has been createdServlet
has been determined from the combination ofFilter
andServlet
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?)