Difference between RequestDispatcher and sendRedirect

 

Forward()
SendRediret()
When we use forward method request is transfer to other resource within the same server for further processing.
In case of sendRedirect request is transfer to another resource to different domain or different server for futher processing.
In case of forward Web container handle all process internally and client or browser is not involved.
When you use SendRedirect container transfers the request to client or browser so url given inside the sendRedirect method is visible as a new request to the client.
When forward is called on requestdispatherobject we pass request and response object so our old request object is present on new resource which is going to process our request
In case of SendRedirect call old request and response object is lost because it’s treated as new request by the browser.
Visually we are not able to see the forwarded address, its is transparent
In address bar we are able to see the new redirected address it’s not transparent.
Using forward () method is faster then send redirect.
SendRedirect is slower because one extra round trip is required beasue completely new request is created and old request object is lost.Two browser request requird.
When we redirect using forward and we want to use same data in new resource we can use request.setAttribute () as we have request object available.
But in sendRedirect if we want to use we have to store the data in session or pass along with the URL.

 

Difference between request.getRequestDispatcher and servletContext.getRequestDispatcher

First we will look at the differences between the two.
request.getRequestDispatcher(String path)
path need not begin with ‘/’. If path starts with ‘/’ then this also works like
servletContext.getRequestDispatcher(String path) 

This can accept path relative to request path or relative to context root path.

Ex: 1
In request, http://localhost:8080/RequestDispatcherExampleApp/s1/page1.jsp
Context root is /RequestDispatcherExampleApp
Relative path to request is /RequestDispatcherExampleApp/s1/

So, request.getRequestDispatcher(“page2.jsp”) while processing the request for
http://localhost:8080/RequestDispatcherExampleApp/s1/page1.jsp will look for page2.jsp under directory /RequestDispatcherExampleApp/s1/page2.jsp

Ex: 2
In request, http://localhost:8080/RequestDispatcherExampleApp/s1/s2/page1.jsp
Context root is /RequestDispatcherExampleApp
Relative path to request is /RequestDispatcherExampleApp/s1/s2/

So, request.getRequestDispatcher(“../page2.jsp”) while processing the request for
http://localhost:8080/RequestDispatcherExampleApp/s1/page1.jsp will look for page2.jsp under directory /RequestDispatcherExampleApp/s1/page2.jsp

From above example it is clear that request.getRequestDispatcher can process resource to the relative path of request URL.

Ex: 3
In request, http://localhost:8080/RequestDispatcherExampleApp/s1/s2/page1.jsp
Context root is /RequestDispatcherExampleApp
Relative path to request is /RequestDispatcherExampleApp/s1/s2/

So, request.getRequestDispatcher(“/page2.jsp”) while processing the request for
http://localhost:8080/RequestDispatcherExampleApp/s1/page1.jsp will look for page2.jsp under                                            directory /RequestDispatcherExampleApp/

From above example it is clear that request.getRequestDispatcher can process resource to the relative path of context root of application.

2. servletContext.getRequestDispatcher(String path)
path should begin with ‘/’.

If the path does not start with ‘/’, you will get following exception
java.lang.IllegalArgumentException: Path does not start with a “/” character.
This can accept path relative to context root of the application.

 

In request,

http://localhost:8080/RequestDispatcherExampleApp/s2/page1.jsp

Context root is /RequestDispatcherExampleApp
Relative path to context root is /RequestDispatcherExampleApp/

So, servletContext.getRequestDispatcher(“/page2.jsp”) while processing the request for  http://localhost:8080/RequestDispatcherExampleApp/s2/page1.jsp will look for page2.jsp under directory /RequestDispatcherExampleApp/page2.jsp

 

 

for Reference Please look into this:

http://java-fries.com/j2ee/10-difference-between-request-getrequestdispatcher-and-context-getrequestdispatcher