Difference between response.sendRedirect() and RequestDispatcher


Web tier: servlets, JSP, Web frameworks: Difference between response.sendRedirect() and RequestDispatcher

  1. Hi,

    can anybody tell me, what the difference between
    response.sendRedirect() and a RequestDispatcher is?

    When do I use which? Do they have a different behaviour
    or purpose?

    Any help is appreciated!


  2. If you use RequestDispatcher, you can forward the HttpRequest and HttpResponse objects to the destination servlet or JSP page. Whereas when you use sendRedirect, the request and response objects are lost and it is a mere redirection to another page.
  3. When the servlet redirects to other resource(sendReDirect()) the request object does not reach the second source directly. The server sends an 302 message to the client saying that the resource is moved to another URL and the client should access from there. The bottom line the life cycle of initial request object terminates with end of first servlet.
    In request disptacher forward method the request object is forwarded to the second resource thus maintaining the object bindings with out round trip to the client on the network. Servlets/ jsps use forward mechanism to delegate taks among the resources.

  4. I sightly suggest one another difference are as per Use:

    sendRedirect()  - when two servlets resides in same applications or in other different application.


    RequestDispatcher  -Only when two servlets are in same application.