JSP中response.sendRedirect()與request.getRequestDispatcher().forward(request,response)這兩個對象都可以使頁面跳轉(zhuǎn),但是二者是有很大的區(qū)別的,分條來說,有以下幾點: ①response.sendRedirect(url)-----重定向到指定URL request.getRequestDispatcher(url).forward(request,response) -----請求轉(zhuǎn)發(fā)到指定URL ②response.sendRedirect(url)-----是客戶端跳轉(zhuǎn) request.getRequestDispatcher(url).forward(request,response) -----是服務(wù)器端跳轉(zhuǎn) ③response.sendRedirect(url)跳轉(zhuǎn)到指定的URL地址后,上個頁面(跳轉(zhuǎn)之前的原來頁面)中的請求全部結(jié)束,原request對象將會消亡,數(shù)據(jù)將會消失。緊接著,當(dāng)前新頁面會新建request對象,即產(chǎn)生新的request對象。 【詳細(xì)過程:redirect 會首先發(fā)一個response給瀏覽器,然后瀏覽器收到這個response后再發(fā)一個requeset給服務(wù)器,服務(wù)器接收后發(fā)新的response給瀏覽器。這時頁面從瀏覽器獲取來的是一個新的request。這時,在原來跳轉(zhuǎn)之前的頁面用request.setAttribute存的東西都沒了,如果在當(dāng)前的新頁面中用request.getAttribute取,得到的將會是null?!?/span> request.getRequestDispatcher(url).forward(request,response)是采用請求轉(zhuǎn)發(fā)方式,在跳轉(zhuǎn)頁面的時候是帶著原來頁面的request和response跳轉(zhuǎn)的,request對象始終存在,不會重新創(chuàng)建。 【詳細(xì)過程:forward 發(fā)生在服務(wù)器內(nèi)部, 是在瀏覽器完全不知情的情況下發(fā)給了瀏覽器另外一個頁面的response. 這時頁面收到的request不是從瀏覽器直接發(fā)來的,可能是在轉(zhuǎn)頁時己經(jīng)用request.setAttribute在request里放了數(shù)據(jù),在轉(zhuǎn)到的頁面就可以直接用request.getAttribute獲得數(shù)據(jù)了?!?/span> ④使用response.sendRedirect()地址欄中的網(wǎng)址將改變 使用request.getRequestDispatcher().forward(request,response)地址欄中的網(wǎng)址保持不變。 ⑤使用response.sendRedirect()時如果需要傳遞參數(shù),那只能在url后加參數(shù),如:url?id=1,而不能通過request或response方式。 使用request.getRequestDispatcher().forward(request,response)如果需要傳遞參數(shù),可以在程序內(nèi)通過response.setAttribute("name",name)來傳至下一個頁面.而不能在后面帶參數(shù)傳遞,比如servlet?name=frank這樣不行。 ⑥運(yùn)用sendRedirect()方法可以讓你重定向到任何URL,而forward()方法只能重定向到同一個Web應(yīng)用程序中的某個資源。 表單form中的action="/uu";sendRedirect("/uu");表示相對于服務(wù)器根路徑。如服務(wù)器根路徑是http://localhost:8080/Test則提交至http://localhost:8080/uu;而Forward代碼中的"/uu"則代表相對于WEB應(yīng)用的路徑。如http://localhost:8080/Test應(yīng)用則提交至http://localhost:8080/Test/uu。 ⑦運(yùn)用HttpServletResponse接口的sendRedirect()方法 sendRedirect()是在用戶的瀏覽器端工作,同時它可以重定向至不同的主機(jī)上,sendRedirect()可以重定向有frame的jsp文件。 假設(shè)轉(zhuǎn)發(fā)代碼包含于注冊的servlet-url為/ggg/tt;jsp為/ggg/tt.jsp。 絕對路徑:response.sendRedirect("http://www.")發(fā)送至http://www. 根路徑:response.sendRedirect("/ooo")發(fā)送至http://localhost:8080/ooo 相對路徑:response.sendRedirect("ooo")發(fā)送至http://localhost:8080/Test/ggg/ooo。 sendRedirect等同于此方式: response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn = "/newpath/jsa.jsp"; response.setHeader("Location",newLocn); ⑧運(yùn)用RequestDispatcher接口的Forward()方法forward()無法重定向至有frame的jsp文件,可以重定向至有frame的html文件, 只有在客戶端沒有輸出時才可以調(diào)用forward方法。如果當(dāng)前頁面的緩沖區(qū)(buffer)不是空的,那么你在調(diào)用forward方法前必須先清空緩沖區(qū)。 "/"代表相對與web應(yīng)用路徑 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為一個<servlet-name>) rd.forward(request, response);提交至名為TestServlet的servlet
如果在<jsp:forward>之前有很多輸出,前面的輸出已使緩沖區(qū)滿,將自動輸出到客戶端,那么該語句將不起作用,這一點應(yīng)該特別注意。 另外要注意:它不能改變?yōu)g覽器地址,刷新的話會導(dǎo)致重復(fù)提交。 從http://localhost:8080/Test/gw/page.jsp中轉(zhuǎn)發(fā) <jsp:forward page="OtherPage.jsp"/>在JSP頁面被解析后轉(zhuǎn)換成pageContext.forward("OtherPage.jsp"); "/OtherPage.jsp"提交到http://localhost:8080/Test/OtherPage.jsp "OtherPage.jsp"提交到http://localhost:8080/Test/gw/OtherPage.jsp
此外,除了這兩種頁面跳轉(zhuǎn)方法,還有一種比較方便的方法: Meta Refresh方法 這種方法是由HTML提供的,Meta本身就是HTML標(biāo)簽。使用方法是: <meta http-equiv="refresh" content="5; url=http://www./" /> 相應(yīng)的java代碼是: String content=stayTime+";URL="+URL; response.setHeader("REFRESH",content); |
|