JSP跳轉(zhuǎn)的方式常用的跳轉(zhuǎn)方式有以下幾種:
(1)href超鏈接標(biāo)記,屬于客戶端跳轉(zhuǎn)
(2)使用javascript完成,屬于客戶端跳轉(zhuǎn)
(3)提交表單完成跳轉(zhuǎn),屬于客戶端跳轉(zhuǎn)
(4)使用response對(duì)象,屬于客戶端跳轉(zhuǎn)
(5)使用requestDispatcher類,屬于服務(wù)器跳轉(zhuǎn)
(1)href超鏈接標(biāo)記
? ? ? ?這個(gè)比較簡(jiǎn)單,通常寫(xiě)到a標(biāo)簽里即可,來(lái)完成指定位置的動(dòng)態(tài)跳轉(zhuǎn)比較方便
? ? ? ?<a href=”new.jsp”>跳轉(zhuǎn)</a>
(2)使用javascript完成? ? ??
? ? ? ? ? (i)可以在JavaScript代碼里寫(xiě)提交表單的寫(xiě)法
?<scripttype="text/javascript"> ?
function?submit()?{??
with(document.getElementById("queryFunction"))?{??
action="new.jsp";??
method="post";??
????????????????????????????submit();??
?????????????????????}??
???????????}??
</script?>??
? ? ? ? ? (ii)也可以直接定位, 給window.location屬性賦值,而不提交表單
?<script??type="text/javascript"> ?
function??go?{??
window.self.location?=?"new.jsp”;??
?????????????}??
</script?>?
或者
<script??type="text/javascript">??
window.location.replace("http://www.baidu.com");??
</script>??
? replace這種方法與定位window.location差別是他沒(méi)有歷史記錄.
? ? ? ? ? ?(iii)使用history對(duì)象的forward(),back(),go()方法
? ? ? ? ? ? ? 其中g(shù)o()方法需要一個(gè)整形入口參數(shù)
? ? ? ? ? ? ? <a ? href="javascript:history.go(-1)">返回上一步</a>?等價(jià)于<a ?href="javascript:history.back()">返回上一步</a>
(3)提交表單完成跳轉(zhuǎn)
? ? ? ? ? ?將整個(gè)form表單數(shù)據(jù)提交的方式.
<form?name="form1"method="POST"?action="new.jsp"> ??
<input?type="text"?name="name">???
<input?type="text"?name="psd">???
<input?type="submit"?value="提交">???
<input?type="reset"?value="重置">???
</from>??
(4)使用response對(duì)象,為Jsp的內(nèi)置對(duì)象
? ? ? ? ?(i)直接使用sendRedirect()重定向, 重定向后在瀏覽器地址欄上會(huì)出現(xiàn)重定向頁(yè)面的URL.代碼:??
? ? ? ? ?<% ?
response.sendRedirect("http://www.baidu.com");?
? ? ? ? ? ? return;? ??????
? ? ? ? ?%>?
? ? ? ? 說(shuō)明: sendredirect()中的URL是可以帶參數(shù)的,例如sendredirect("url?name="+name);我們可以在跳轉(zhuǎn)的時(shí)候傳入?yún)?shù).
? ? ? ? 此外,一般response.sendRedirect()之后緊跟一句 return;我們已經(jīng)知道response.sendRedirect是通過(guò)瀏覽器來(lái)做轉(zhuǎn)向的,所以只有在頁(yè)面處理完成后,才會(huì)有實(shí)際的動(dòng)作。既然已經(jīng)要做轉(zhuǎn)向了,那么后的輸出就已經(jīng)沒(méi)有意義了,而且有可能會(huì)因?yàn)楹竺娴妮敵鰧?dǎo)致轉(zhuǎn)向失敗。
? ? ? ? ?(ii)使用setHeader()方法,直接修改地址欄來(lái)實(shí)現(xiàn)頁(yè)面的重定向
? ? ? ? ?<% ?
? ? ? ? ? ? ? ?response.setHeader("Refresh","1;url=http://www.baidu.com");?????? ?
? ? ? ? ?%>?
? ? ? ? ?標(biāo)準(zhǔn)格式: response.setHeader("Refresh","等待的秒數(shù);url=絕對(duì)路徑或者相對(duì)路徑");上例是等待1秒之后跳轉(zhuǎn).
(5)使用requestDispatcher類
? ? ? ? ? 基本方法
RequestDispatcher??rd?=?request.getRequestDispatcher("目標(biāo)頁(yè)面");??
rd.forward(response,request);??
? ? ? ? ? Servlet可通過(guò)兩種方式得到RequestDispatcher對(duì)象:
? ? ? ? ? ServletContext的getRequestDispatcher()????
? ? ? ? ? ServletRequest的getRequestDispatcher()??
? ? ? ? ?調(diào)用ServletContext的getRequestDispatcher(String path)方法,path參數(shù)指定目標(biāo)組件的路徑。
? ? ? ? ?調(diào)用ServletRequest的getRequestDispatcher(String path)方法與上一個(gè)方式的區(qū)別在于,前者的path參數(shù)必須是絕對(duì)路徑,而后者的path參數(shù)可以是絕對(duì)路徑,也可以是相對(duì)路徑。所謂絕對(duì)路徑,就是指以符號(hào)"/"開(kāi)頭的路徑,"/"表示當(dāng)前web應(yīng)用的URL入口。
??最后比較一下response重定向和forward跳轉(zhuǎn)的區(qū)別
response: ? ?1執(zhí)行完所有的代碼再跳轉(zhuǎn)到目標(biāo)頁(yè)?
? ? ? ? ? ? ? ? ? ? ? ? ?2跳轉(zhuǎn)到目標(biāo)頁(yè)后瀏覽器的URL會(huì)改變
? ? ? ? ? ? ? ? ? ? ? ? ?3在瀏覽器中重定向
? ? ? ? ? ? ? ? ? ? ? ? ? 4可以跳轉(zhuǎn)到其他服務(wù)器上的頁(yè)面,例如”百度”。
forward跳轉(zhuǎn) : 1直接跳轉(zhuǎn)到目標(biāo)網(wǎng)頁(yè) 其后的代碼不再執(zhí)行
? ? ? ? ? ? ? ? ? ? ? ? 2跳轉(zhuǎn)到目標(biāo)頁(yè)后URL不變
? ? ? ? ? ? ? ? ? ? ? ? 3在服務(wù)器端重定向
? ? ? ? ? ? ? ? ? ? ? ? 4無(wú)法跳轉(zhuǎn)到其他服務(wù)器上的頁(yè)面