What's the difference between RequestDispatcher.forward() and HttpServletResponse.sendRedirect()?

54,612

Solution 1

Redirection is a type of response sent back to the client, whereas the forward delegation takes place completely on the server side and the result of the forward action returns to the client as if it came only from the original URL.

Another difference is that forward delegation can be used only for in-applications resources, whereas redirection command can redirect the client browser outside the current domain.

Examples:

// Sends a temporary redirect to the HTTP client. Only absolute URLs are allowed.
ServletResponse.sendRedirect(String location);


// Delegates one HttpRequest to another dynamic or static resource
HttpRequest.getRequestDispatcher("example.jsp").forward(request, response);


// Includes/enriches current response with another dynamic or static resource
HttpRequest.getRequestDispatcher("example.html").include(request, response);


Another good explanation can be found here:
Difference between sendRedirect() and forward()

Solution 2

SendRedirect ():
This method is declared in HttpServletResponse Interface

Signature: void sendRedirect(String url)

This method is used to redirect client request to some other location for further processing ,the new location is available on different server or different context.our web container handle this and transfer the request using browser ,and this request is visible in browser as a new request. Some time this is also called as client side redirect.

Forward(): This method is declared in RequestDispatcher Interface.

Signature: forward(ServletRequest request, ServletResponse response)

This method is used to pass the request to another resource for further processing within the same server, another resource could be any servlet, jsp page any kind of file.This process is taken care by web container when we call forward method request is sent to another resource without the client being informed, which resource will handle the request it has been mention on requestDispatcher object which we can get by two ways either using ServletContext or Request. This is also called server side redirect.

A RequestDispatcher forward() is used to forward the same request to another resource whereas ServletResponse sendRedirect() is a two step process. In sendRedirect(), web application returns the response to client with status code 302 (redirect) with URL to send the request. The request sent is a completely new request.

B forward() is handled internally by the container whereas sednRedirect() is handled by browser.

C We should use forward() when accessing resources in the same application because it’s faster than sendRedirect() method that required an extra network call.

D In forward() browser is unaware of the actual processing resource and the URL in address bar remains same whereas in sendRedirect() URL in address bar change to the forwarded resource.

E forward() can’t be used to invoke a servlet in another context, we can only use sendRedirect() in this case.

Details Explanation Here

Solution 3

We can use request dispatcher only when the other servlet to which the request is being forwarded lies in the same application. On the other hand Send Redirect can be used in both the cases if the two servlets resides in a same application or in different applications.

Solution 4

A request is forwarded only forwards the request to the same WEB application components, and redirection can also be redirected to the same site in different application resources, and even can be directed to an absolute URL.

Redirection can see the URL of the target page, the page URL forwarding can only see the first visit, after all there is a server to do the work.

Request response between caller and callee objects share the same request and response objects, redirect the caller and callee belong to two separate access request and response process.

Must be added after the jump redirect return, or else jump though the page, but also performs jump behind the statement, forwarding is performed jump page, the following code would not be executed.

Share:
54,612

Related videos on Youtube

Raj
Author by

Raj

Updated on July 09, 2020

Comments

  • Raj
    Raj almost 4 years

    What is the difference between RequestDispatcher's forward() and HttpServletResponse's sendRedirect() method?
    Can anyone explain with a example and best usage of these methods with a real time example?

  • KNU
    KNU about 10 years
    Note: you can do response.sendRedirect("http://www.google.com"); but not HttpRequest.getRequestDispatcher("http://www.google.com"); unless google.com resides on your server. CONCLUSION : The forward()method works at server side whereas the sendRedirect() method works at client side.