RequestDispatcher.forward() 与 HttpServletResponse.sendRedirect() [英] RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()

查看:23
本文介绍了RequestDispatcher.forward() 与 HttpServletResponse.sendRedirect()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

forward()sendRedirect() 在概念上有什么区别?

What is the conceptual difference between forward() and sendRedirect()?

推荐答案

requestDispatcher - forward() 方法

  1. 当我们使用 forward 方法时,请求被传输到同一服务器内的另一个资源以供进一步处理.

  1. When we use the forward method, the request is transferred to another resource within the same server for further processing.

forward的情况下,Web容器在内部处理所有处理,不涉及客户端或浏览器.

In the case of forward, the web container handles all processing internally and the client or browser is not involved.

当在 requestDispatcher 对象上调用 forward 时,我们传递请求和响应对象,所以我们的旧请求对象出现在新资源上将处理我们的请求.

When forward is called on the requestDispatcherobject, we pass the request and response objects, so our old request object is present on the new resource which is going to process our request.

在视觉上,我们无法看到转发的地址,它是透明的.

Visually, we are not able to see the forwarded address, it is transparent.

使用 forward() 方法比 sendRedirect 更快.​​

Using the forward() method is faster than sendRedirect.

当我们使用 forward 重定向时,我们想在新资源中使用相同的数据,我们可以使用 request.setAttribute(),因为我们有一个可用的请求对象.p>

When we redirect using forward, and we want to use the same data in a new resource, we can use request.setAttribute() as we have a request object available.

发送重定向

  1. sendRedirect 的情况下,请求被转移到另一个资源、不同的域或不同的服务器进行进一步处理.

  1. In case of sendRedirect, the request is transferred to another resource, to a different domain, or to a different server for further processing.

当您使用 sendRedirect 时,容器会将请求传输到客户端或浏览器,因此 sendRedirect 方法中给出的 URL 作为新请求可见给客户.

When you use sendRedirect, the container transfers the request to the client or browser, so the URL given inside the sendRedirect method is visible as a new request to the client.

sendRedirect 调用的情况下,旧的请求和响应对象将丢失,因为浏览器将其视为新请求.

In case of sendRedirect call, the old request and response objects are lost because it’s treated as new request by the browser.

在地址栏中,我们可以看到新的重定向地址.它不透明.

In the address bar, we are able to see the new redirected address. It’s not transparent.

sendRedirect 较慢,因为需要一次额外的往返,因为创建了一个全新的请求并且旧的请求对象丢失.需要两个浏览器请求.

sendRedirect is slower because one extra round trip is required, because a completely new request is created and the old request object is lost. Two browser request are required.

但是在 sendRedirect 中,如果我们想对新资源使用相同的数据,我们必须将数据存储在会话中或与 URL 一起传递.

But in sendRedirect, if we want to use the same data for a new resource we have to store the data in session or pass along with the URL.

哪个好?

这取决于哪种方法更有用的场景.

Which one is good?

Its depends upon the scenario for which method is more useful.

如果您希望将控制权转移到新的服务器或上下文,并将其视为全新的任务,那么我们选择 sendRedirect.通常,如果在浏览器重新加载网页时可以安全地重复操作并且不会影响结果,则应使用转发.

If you want control is transfer to new server or context, and it is treated as completely new task, then we go for sendRedirect. Generally, a forward should be used if the operation can be safely repeated upon a browser reload of the web page and will not affect the result.

来源

这篇关于RequestDispatcher.forward() 与 HttpServletResponse.sendRedirect()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆