Search code examples
javajspjakarta-eeservletsrequestdispatcher

A java class method can call another java class method. Then why do you need RequestDispatcher?


Servlets are java classes. A java class method can call another java class method. Then why do you need RequestDispatcher?


Solution

  • With a request dispatcher you specify the resource to include or forward to as a URI so you can dispatch to resources other than java classes, for example a static file, a jsp, a jsf page, or another servlet which your java class does not have access to.

    Also the dispatcher enforces rules such as an included resource cannot set a header and the response is completed on return from forward.

    If you don't need these rules enforced and you only call well behaved available java classes then I agree you don't need to use a request dispatcher.