What is a request dispatcher and how does it work?
A RequestDispatcher object can forward a client’s request to a resource or include the resource itself in the response back to the client. A resource can be another servlet, or an HTML file, or a JSP file, etc. 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. To illustrate, suppose you want Servlet_A to invoke Servlet_B.
Related Questions
- What can I do to assist students with disabilities who are eligible for notetaking services but reluctant to request and recruit a fellow classmate for copies of notes?
- When should an Institutional Review Board (IRB) or institution request a "407" review for research involving children as subjects?
- What is a request dispatcher and how does it work?