Java WEB: what's the difference between redirect and dispatch?

From http://stackoverflow.com/questions/27900017/whats-the-difference-between-redirect-and-dispatch-in-phalcon


Broadly speaking, Redirect will do an http redirection (with the header location). It means that the browser of the client will change the page. It processes to a new routing (it can also be used to go to another website) and the actual script will end.

Whereas Forward is internal, the browser of the client wont see any difference, you just execute a different controller.The dispatch loop allows the user to forward the execution flow to another controller/action. This is very useful to check if the user can access to certain options, redirect users to other screens or simply reuse code. But, Keep in mind that making a “forward” is not the same as making an HTTP redirect. Although they apparently got the same result. The “forward” doesn’t reload the current page, all the redirection occurs in a single request, while the HTTP redirect needs two requests to complete the process.

Similar analogy can be shown in JSP as well,

The sendRedirect(String path) method of HttpServletResponse will tell the client that it should send a request to the specified path. So the client will build a new request and submit it to the server. The client’s history will be updated so the forward and back buttons will work. This method is useful for redirecting to pages on other servers and domains.

The forward method of RequestDispatcher will forward the ServletRequest and ServletResponse that it is passed to the path that was specified in getRequestDispatcher(String path). The response will not be sent back to the client and so the client will not know about this change of resource on the server. This method is useful for communicating between server resources, (servlet to servlet). This method is faster than using sendRedirect as no network round trip to the server and back is required.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值