您的位置:首页 > Web前端

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

2015-08-07 21:38 381 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: