sendRedirect或请求分派更有效吗? [英] sendRedirect or request Dispatch is more efficient?

查看:58
本文介绍了sendRedirect或请求分派更有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SendRedirect或requestdispatch?哪个应该更受青睐?哪个效率更高?

SendRedirect or requestdispatch ?Which should be more preferred?Which is more efficient?

推荐答案

它们做两件非常不同的事情,所以您不能仅仅决定效率.

They do two very different things, so you cannot just decide on efficiency.

发送重定向会将浏览器发送到其他URL.该URL将对浏览器可见.您可能想要也可能不想要.例如,在POST之后,您可能应该重定向到GET页面,以避免不重新发布就无法重新加载结果页面.另一方面,您不能重定向到只能从servlet容器内部访问的页面".

Sending a redirect will send the browser to a different URL. That URL will be visible to the browser. You may or may not want that. For example, after a POST, you should probably redirect to a GET page to avoid that the result page cannot be reloaded without re-posting. On the other hand, you cannot redirect to "pages" that are only accessible from inside the servlet container.

调度是更有效的",因为它没有额外的往返,但是它只能与相同的Web应用程序上下文一起使用(或者,如果您这样设置的话,则最多只能在同一个servlet容器内).同样,用户最初用于访问页面的URL与后面称为servlet的URL会有所不同,这可能会造成混淆.调度模式通常用于实际请求之前或之后的额外处理(代替ServletFilter),或用于错误页面.

A dispatch is more "efficient" in that there is no extra round-trip, but it only works withing the same web-application context (or at most within the same servlet container if you so set it up). Also, the URL that the user used to access the page in the first place will be different from what a servlet later on in the chain will be called as, which may be confusing. The dispatch pattern is often used for extra processing before or after the real request (in lieu of a ServletFilter), or for error pages.

您可以使用分派传递请求属性,但只能在重定向上传递查​​询参数.您不能作为POST重定向(因此,可以附加到它的数据量是有限的).重定向中的所有查询参数对用户都是可见的.

You can pass along request attributes using a dispatch, but only query parameters on a redirect. You cannot redirect as a POST (so the amount of data that you can attach to it is limited). All query parameters in a redirect are visible to the user.

这篇关于sendRedirect或请求分派更有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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