What is a request dispatcher and how does it work?
You can also think of a RequestDispatcher object as a wrapper for the resource located at a given path that is supplied as an argument to the getRequestDispatcher method.
For constructing a RequestDispatcher object, you can use either the ServletRequest.getRequestDispatcher() method or the ServletContext.getRequestDispatcher() method. They both do the same thing, but impose slightly different constraints on the argument path. For the former, it looks for the resource in the same webapp to which the invoking servlet belongs and the pathname specified can be relative to invoking servlet. For the latter, the pathname must begin with '/' and is interpreted relative to the root of the webapp.
RequestDispatcher dispatcher = getRequestDispatcher("Servlet_B"); dispatcher.forward( request, response );where request, of type HttpServletRequest, is the first parameter of the enclosing service method (or the doGet method) and response, of type HttpServletResponse, the second. You could accomplish the same by
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( "/servlet/Servlet_B" ); dispatcher.forward( request, response );