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

查看:93
本文介绍了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更快.

当我们使用转发进行重定向,并且希望在新资源中使用相同的数据时,可以使用request.setAttribute(),因为我们有一个可用的请求对象.

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.

SendRedirect

SendRedirect

  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天全站免登陆