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.
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.
相关文章推荐
- XML基础,与HTML的对比中学习
- JSP之九大内置对象
- Json解析完全解析
- HTML控件和WEB控件的区别和联系
- LeetCode222 Count Complete Tree Nodes
- json 接口定义坑
- javascript中的defer属性和async属性
- Day1_HTML_表格
- HDOJ 1015 Safecracker(DFS)
- jQuery基础学习一 简介、选择器、事件
- 《JavaScript》——面向对象之原型
- 【leetcode每日一题】NO19.Remove Nth Node From End of List
- js使用经验之谈
- 将Visual Studio打造成为Node.js IDE
- 前端页面一些应用
- 关于混合开发,oc与js互相通信的方法总结
- Bootstrap的js插件之警告框(alert.js)
- muduo::Buffer分析
- 全方位了解CSS3的Regions扩展
- npm常用命令->nodejs