使用jsp大約有下列三種跳轉方式:
1. response.sendredirect();
2. response.setheader("location","");
3. <jsp:forward page="" />
經過試驗得到下面的一些規則:
一. response.sendredirect()
此語句前不允許有out.flush(),如果有,會有異常:
java.lang.illegalstateexception: can't sendredirect() after data has committed to the client.
at com.caucho.server.connection.abstracthttpresponse.sendredirect(abstracthttpresponse.java:558)
...
跳轉后瀏覽器地址欄變化
如果要跳到不同主機下,跳轉后,此語句后面的語句會繼續執行,如同新開了線程,但是對response的操作已經無意義了;
如果要跳到相同主機下,此語句后面的語句執行完成后才會跳轉;
二. response.setheader("location","")
此語句前不允許有out.flush(),如果有,頁面不會跳轉。
跳轉后瀏覽器地址欄變化
此語句后面的語句執行完成后才會跳轉
三. <jsp:forward page="" />
此語句前不允許有out.flush(),如果有,會有異常:
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)
...
跳轉后瀏覽器地址欄不變,但是只能跳到當前主機下
此語句后面的語句執行完成后才會跳轉
新聞熱點
疑難解答