Ticket #79 (new enhancement)
老程序员的页面跳转写法
| Reported by: | liaojunqiang | Owned by: | |
|---|---|---|---|
| Priority: | trivial | Milestone: | |
| Component: | 系统相关 | Version: | |
| Keywords: | http 页面跳转 | Cc: | |
| Due Date: | 17/01/2012 |
Description (last modified by liaojunqiang) (diff)
整理接口的时候发现一个老程序员写的页面跳转方法,觉得挺有趣的,所以在这里分享一下。
response.setStatus(301);
response.setHeader("Location", newurl);
response.setHeader("Connection", "close");
一般程序员是这样写的
response.sendRedirect(newurl);
第2种比第1种要好,因为:
1,代码更加容易阅读,简洁。
2,兼容老式浏览器,有些浏览器是不支持自动重定向,可能现在也不多见了。
3,能够把相对URL转换成绝对URL,可移植性强。
如果觉得第一种比第二种好的童鞋请指教,欢迎拍砖...
Change History
comment:2 Changed 14 years ago by chenchongqi
这两种跳转的本质上没有区别,都是返回一个跳转状态和新url给浏览器,然后浏览器再跳转,但是返回的响应代码有区别。
第二种我实际观察,实际上是发了302的响应代码,两种代码的区别请看:
http://rdwiki.pc.com.cn/pages/viewpage.action?pageId=1016040&focusedCommentId=1016238#comment-1016238
另外一种跳转是服务器端直接forward,那就不关浏览器的事了,地址栏里的url也不会变。
Note: See
TracTickets for help on using
tickets.
![(please configure the [header_logo] section in trac.ini)](http://www1.pconline.com.cn/global/2008/images/jss/m_logo091125.jpg)