AsyncContext Interface dispatch method
In this section, you will learn about dispatch method of AsyncContext Interface.
The AsyncContext Interface dispatch() method is of two
- void dispatch()
The dispatch method takes no argument.
It uses the original URI as the path. If the AsyncContext was initialized
startAsync(ServletRequest,ServletResponse) and the request passed is
an instance of HttpServletRequest, then the dispatch is to the URI returned
by HttpServletRequest.getRequestURI(). Otherwise the dispatch is to the URI of
the request when it was last dispatched by the container.
- void dispatch(java.lang.String path)
The dispatch method takes a
String argument as path which must be inside the scope of the ServletContext. This path must be relative to the root of the
and begin with a ?/?.
- void dispatch(ServletContext context, java.lang.String path)
The dispatch method accepts a String argument describing a path
within the scope of the ServletContext specified. This path must be
relative to the root of the ServletContext specified and should
starts with a ?/?.
Given below a sample snippet using dispatch() method :
Consider a scenario where we have three Servlets - X , Y and Z which have
URLs : /x , /y & /z :
Within Servlet X, we have :
Within Servlet Y, we have :
Within Servlet Z, we have following two statements :
request.startAsync().dispatch(); // to /y
It will dispatch to the last dispatched URI, which relates to /y.
request.startAsync(request, response).dispatch(); // to /z
It will dispatch to request.getRequestURI(), which corresponds to /z.