RequestDispatcher.forward() 和 HttpServletResponse.sendRedirect() 有什么区别?
RequestDispatcher
的 forward()
和 HttpServletResponse
的 sendRedirect()
方法有什么区别?
任何人都可以用一个例子来解释这些方法的最佳用法和实时例子吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
重定向是一种发送回客户端的响应,而转发委托完全发生在服务器端,并且转发操作的结果返回到客户端,就好像它仅来自原始 URL 一样。
另一个区别是前向委派只能用于应用程序内的资源,而重定向
命令可以将客户端浏览器重定向到当前域之外。
示例:
Another good explanation can be found here:
Difference between sendRedirect() and forward()
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:
Another good explanation can be found here:
Difference between sendRedirect() and forward()
发送重定向 ():
该方法在 HttpServletResponse 接口中声明
该方法用于将客户端请求重定向到其他位置以进行进一步处理,新位置在不同服务器或不同上下文上可用。我们的 Web 容器处理此问题并使用浏览器传输请求,并且此请求在浏览器中作为新请求可见。有时这也称为客户端重定向。
转发():
该方法在RequestDispatcher接口中声明。
该方法用于将请求传递给同一服务器内的另一个资源进行进一步处理,另一个资源可以是任何servlet、jsp页面任何类型的文件。当我们调用forward方法时,这个过程由Web容器负责,请求被发送到另一个资源而不通知客户端,哪个资源将处理请求,它已经在requestDispatcher对象上提到了,我们可以通过两种方式获取:使用ServletContext或Request 。这也称为服务器端重定向。
RequestDispatcherforward() 用于将相同的请求转发到另一个资源,而 ServletResponsesendRedirect() 是一个两步过程。在 sendRedirect() 中,Web 应用程序将响应返回给客户端,状态代码为 302(重定向)以及发送请求的 URL。发送的请求是一个全新的请求。
Bforward() 由容器内部处理,而 sendRedirect() 由浏览器处理。
C 当访问同一应用程序中的资源时,我们应该使用forward(),因为它比需要额外网络调用的sendRedirect()方法更快。
D 在forward()中,浏览器不知道实际处理的资源,并且地址栏中的URL保持不变,而在sendRedirect()中,地址栏中的URL更改为转发的资源。
Eforward() 不能用于在另一个上下文中调用 servlet,在这种情况下我们只能使用 sendRedirect()。
此处有详细说明
SendRedirect ():
This method is declared in HttpServletResponse Interface
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 handles this and transfers the request using browser, and this request is visible in the browser as a new request. Sometimes this is also called as client side redirect.
Forward():
This method is declared in RequestDispatcher Interface.
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 sendRedirect() 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
仅当请求转发到的其他 servlet 位于同一应用程序中时,我们才能使用请求调度程序。另一方面,如果两个 servlet 驻留在同一应用程序或不同应用程序中,则可以在这两种情况下使用发送重定向。
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.
请求转发只是将请求转发到同一个WEB应用组件,而重定向也可以重定向到不同应用资源中的同一个站点,甚至可以定向到绝对URL。
重定向可以看到目标页面的URL,页面URL转发只能看到第一次访问,毕竟有服务器来做这项工作。
调用者和被调用者之间的请求响应对象共享相同的请求和响应对象,重定向调用者和被调用者属于两个独立的访问请求和响应过程。
跳转后必须添加重定向返回,否则虽然跳转页面,但后面的语句也会执行跳转,转发是执行跳转页面,下面的代码不会被执行。
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.