使用jsp大約有下列三種跳轉(zhuǎn)方式:
1. response.sendredirect();
2. response.setheader("location","");
3. <jsp:forward page="" />
經(jīng)過(guò)試驗(yàn)得到下面的一些規(guī)則:
一. response.sendredirect()
此語(yǔ)句前不允許有out.flush(),如果有,會(huì)有異常:
java.lang.illegalstateexception: can't sendredirect() after data has committed to the client.
at com.caucho.server.connection.abstracthttpresponse.sendredirect(abstracthttpresponse.java:558)
...
跳轉(zhuǎn)后瀏覽器地址欄變化
如果要跳到不同主機(jī)下,跳轉(zhuǎn)后,此語(yǔ)句后面的語(yǔ)句會(huì)繼續(xù)執(zhí)行,如同新開了線程,但是對(duì)response的操作已經(jīng)無(wú)意義了;
如果要跳到相同主機(jī)下,此語(yǔ)句后面的語(yǔ)句執(zhí)行完成后才會(huì)跳轉(zhuǎn);
二. response.setheader("location","")
此語(yǔ)句前不允許有out.flush(),如果有,頁(yè)面不會(huì)跳轉(zhuǎn)。
跳轉(zhuǎn)后瀏覽器地址欄變化
此語(yǔ)句后面的語(yǔ)句執(zhí)行完成后才會(huì)跳轉(zhuǎn)
三. <jsp:forward page="" />
此語(yǔ)句前不允許有out.flush(),如果有,會(huì)有異常:
java.lang.illegalstateexception: forward() not allowed after buffer has committed.
at com.caucho.server.webapp.requestdispatcherimpl.forward(requestdispatcherimpl.java:134)
at com.caucho.server.webapp.requestdispatcherimpl.forward(requestdispatcherimpl.java:101)
at com.caucho.jsp.pagecontextimpl.forward(pagecontextimpl.java:836)
...
跳轉(zhuǎn)后瀏覽器地址欄不變,但是只能跳到當(dāng)前主機(jī)下
此語(yǔ)句后面的語(yǔ)句執(zhí)行完成后才會(huì)跳轉(zhuǎn)
最大的網(wǎng)站源碼資源下載站,
新聞熱點(diǎn)
疑難解答
圖片精選