Web tier: servlets, JSP, Web frameworks: Difference between response.sendRedirect() and RequestDispatcher
- Posted by: Stephan Zeeh
- Posted on: August 16 2000 10:49 EDT
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
Any help is appreciated!
- Difference between response.sendRedirect() and RequestDispatcher by manikandan chandrasekaran on August 16 2000 12:49 EDT
- Difference between response.sendRedirect() and RequestDispatcher by Ravi Ravipati on August 16 2000 16:46 EDT
- Difference between response.sendRedirect() and RequestDispatcher by mangesh pawar on November 26 2013 08:00 EST
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.
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.
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.