开发者

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

This question already has answers here: R开发者_开发问答equestDispatcher.forward() vs HttpServletResponse.sendRedirect() (9 answers) Closed 4 years ago.

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?


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()


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜