在Java Web開發中,經常會用到跳轉頁面的方法,一般有下面兩種方法。
Java代碼
HttpServletResponse response = new HttpServletResponse();
response.sendRedirect(location)
Java代碼
RequestDispatcher rd = new RequestDispatcher();
rd.forward(request, response)
跳轉方式
http://localhost:8080/Test應用
運用forward方法隻能重定向到同一個Web應用程式中的一個資源。而sendRedirect方法可以讓你重定向到任何URL。
表單form的action= "/uu ";sendRedirect( "/uu ");表示相對于伺服器根路徑。如http://localhost:8080/Test應用(則送出至http://localhost:8080/uu);
Forward代碼中的 "/uu "則代表相對與WEB應用的路徑。如http://localhost:8080/Test應用(則送出至http://localhost:8080/Test/uu);
(運用RequestDispatcher接口的Forward)方法
forward()無法重定向至有frame的jsp檔案,可以重定向至有frame的html檔案,
同時forward()無法在後面帶參數傳遞,比如servlet?name=frank,這樣不行,可以程式内通過response.setAttribute( "name ",name)來傳至下一個頁面.
重定向後浏覽器位址欄URL不變.
隻有在用戶端沒有輸出時才可以調用forward方法。如果目前頁面的緩沖區(buffer)不是空的,那麼你在調用forward方法前必須先清空緩沖區。
"/ "代表相對與web應用路徑 (注意最好帶"/"開始,否則認為路徑相對于原來的請求開始,容易出錯)
RequestDispatcher rd = request.getRequestDispatcher( "/ooo ");
rd.forward(request, response);送出至http://localhost:8080/Test/ooo
RequestDispatcher rd = getServletContext().getRequestDispatcher( "/ooo ");
rd.forward(request, response);送出至http://localhost:8080/Test/ooo
RequestDispatcher rd =getServletContext().getNamedDispatcher( "TestServlet ");(TestServlet為一個 )
rd.forward(request, response);送出至名為TestServlet的servlet
如果在 之前有很多輸出,前面的輸出已使緩沖區滿,将自動輸出到用戶端,那麼該語句将不起作用,這一點應該特别注意。
另外要注意:它不能改變浏覽器位址,重新整理的話會導緻重複送出
從http://localhost:8080/Test/gw/page.jsp中轉發
在JSP頁面被解析後轉換成pageContext.forward( "OtherPage.jsp ");
"/OtherPage.jsp "送出到http://localhost:8080/Test/OtherPage.jsp
"OtherPage.jsp "送出到http://localhost:8080/Test/gw/OtherPage.jsp
(運用HttpServletResponse接口的sendRedirect)方法302
是在使用者的浏覽器端工作,sendRedirect()可以帶參數傳遞,比如servlet?name=frank傳至下個頁面,
同時它可以重定向至不同的主機上,sendRedirect()可以重定向有frame.的jsp檔案.
假設轉發代碼包含于注冊的servlet-url為/ggg/tt;jsp為/ggg/tt.jsp:
絕對路徑:response.sendRedirect( "http://www.brainysoftware.com ")發送至http://www.brainysoftware.com
根路徑:response.sendRedirect( "/ooo ")發送至http://localhost:8080/ooo
相對路徑:response.sendRedirect( "ooo ")發送至http://localhost:8080/Test/ggg/ooo,
sendRedirect等同于此方式
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = "/newpath/jsa.jsp ";
response.setHeader( "Location ",newLocn);
(Meta Refresh)方法200
這種方法是由HTML提供的,Meta本身就是HTML标簽。使用方法是:
相應的java代碼
String content=stayTime+ ";URL= "+URL;
response.setHeader( "REFRESH ",content);
使用response.sendRedirect()位址欄将改變
使用request.getRequestDispatcher().forward(request,response)位址欄中的資訊保持不變
request.setAttribute存的東西
隻用通過方法2跳轉 才能在新頁取出來
redirect 會首先發一個response給浏覽器, 然後浏覽器收到這個response後再發一個requeset給伺服器, 然後伺服器發新的response給浏覽器. 這時頁面收到的request是一個新從浏覽器發來的.
forward 發生在伺服器内部, 在浏覽器完全不知情的情況下發給了浏覽器另外一個頁面的response. 這時頁面收到的request不是從浏覽器直接發來了,可能己經放了資料.
是以:
request.setAttribute存的東西
隻用通過方法2跳轉 才能在新頁取出來
示例:項目的路徑如下,可以看得出login.jsp在WEB-INF下面,而index.jsp在和WEB-INF同一級

我要實作從index.jsp跳轉到login.jsp裡面
index.jsp:
servlet.java如下:
注意:"/"就表示是項目名,其實就是WebContent
public class LoginController extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
System.out.println("enter to doGet");
//注意在字元串裡面//表示一個/
RequestDispatcher view=request.getRequestDispatcher("/WEB-INF/login.jsp");
view.forward(request,response);
}
}
web.xml
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
index.jsp
Login Servlet
com.kedacom.servletdemo.controller.LoginController
Login Servlet
/login
即可實作從index.jsp跳轉到login.jsp頁面
注意:
response.sendRedirect(request.getContextPath()+"/list"); 必須加項目路徑,不然就沒有項目名了,且這個預設為後面發出的請求為GET方法。
重定向請求為:http://localhost:8080/OriginalServletDemo/list
而RequestDispatcher view = request .getRequestDispatcher("/list");
view.forward(request, response);
而這兒路徑"/" 就自動有加項目名字,是以就直接"/list"就行了,而且要注意的是後面處理/list的servlet的處理到底是doGet還是doPost,是有調用這個RequestDispatcher是doPost還是doGet決定的。