AsyncContext Interface dispatch method

In this section, you will learn about dispatch method of AsyncContext Interface.

AsyncContext Interface dispatch method

AsyncContext Interface dispatch method

In this section, you will learn about dispatch method of AsyncContext Interface.

The AsyncContext Interface dispatch() method is of two types :

  • void dispatch()

    The dispatch method takes no argument. It uses the original URI as the path. If the AsyncContext was initialized via the
    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 ServletContext 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 ?/?.

EXAMPLE

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 :

request.getRequestDispatcher("/x").forward(request, response);

Within Servlet Y, we have :

request.getRequestDispatcher("/y").forward(request, response)

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.