forward 和redirect
1. forward方法使用
request.getRequestDispatcher(path).forward(request.response);首先來(lái)看getRequestDispatcher方法,path必須是相對(duì)路徑。
getRequestDispatcher
RequestDispatcher getRequestDispatcher(String?path)The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns?null?if the servlet container cannot return a?RequestDispatcher.
The difference between this method and?ServletContext.getRequestDispatcher(java.lang.String)?is that this method can take a relative path.
?
接著forward方法,
forward
void forward(ServletRequest?request,ServletResponse?response)throws ServletException,IOExceptionFor a?RequestDispatcher?obtained via?getRequestDispatcher(), the?ServletRequest?object has its path elements and parameters adjusted to match the path of the target resource.
forward?should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an?IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of theServletRequestWrapper?or?ServletResponseWrapper?classes that wrap them.
?
2.sendRedirect方法使用
HttpServletResponse中使用sendRedirect可以實(shí)現(xiàn)跳轉(zhuǎn),可以接受相對(duì)路徑或者絕對(duì)路徑。
sendRedirect
public void sendRedirect(java.lang.String?location)throws java.io.IOExceptionIf the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
?
Servlet 跳轉(zhuǎn) redirect與forward跳轉(zhuǎn)的區(qū)別
Servlet:
當(dāng)然,在servlet中,一般跳轉(zhuǎn)都發(fā)生在doGet, doPost等方法里面。
一、原理
1) redirect 方式
response.sendRedirect("/a.jsp");
頁(yè)面的路徑是相對(duì)路徑。sendRedirect可以將頁(yè)面跳轉(zhuǎn)到任何頁(yè)面,不一定局限于本web應(yīng)用中,如:
response.sendRedirect("http://www.ycul.com");
跳轉(zhuǎn)后瀏覽器地址欄變化。
這種方式要傳值出去的話,只能在url中帶parameter或者放在session中,無(wú)法使用request.setAttribute來(lái)傳遞。
這種方式是在客戶端作的重定向處理。該方法通過(guò)修改HTTP協(xié)議的HEADER部分,對(duì)瀏覽器下達(dá)重定向指令的,讓瀏覽器對(duì)在location中指定的URL提出請(qǐng)求,使瀏覽器顯示重定向網(wǎng)頁(yè)的內(nèi)容。該方法可以接受絕對(duì)的或相對(duì)的URLs。如果傳遞到該方法的參數(shù)是一個(gè)相對(duì)的URL,那么Web container在將它發(fā)送到客戶端前會(huì)把它轉(zhuǎn)換成一個(gè)絕對(duì)的URL。public void doPost(HttpServletRequest request,HttpServletResponse response)??? throws ServletException,IOException
{
??????? response.setContentType("text/html; charset=UTF-8");
??????? response.sendRedirect("/index.jsp");
}
2) forward方式
RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");
dispatcher .forward(request, response);
頁(yè)面的路徑是相對(duì)路徑。forward方式只能跳轉(zhuǎn)到本web應(yīng)用中的頁(yè)面上。
跳轉(zhuǎn)后瀏覽器地址欄不會(huì)變化。
使用這種方式跳轉(zhuǎn),傳值可以使用三種方法:url中帶parameter,session,request.setAttribute
這種方式是在服務(wù)器端作的重定向。服務(wù)器往client發(fā)送數(shù)據(jù)的過(guò)程是這樣的:服務(wù)器在向客戶端發(fā)送數(shù)據(jù)之前,是先將數(shù)據(jù)輸出到緩沖區(qū),然后將緩沖區(qū)中數(shù)據(jù)發(fā)送給client端。什么時(shí)候?qū)⒕彌_區(qū)里的數(shù)據(jù)發(fā)送給client端呢?(1)當(dāng)對(duì)來(lái)自client的request處理完,并把所有數(shù)據(jù)輸出到緩沖區(qū),(2)當(dāng)緩沖區(qū)滿,(3)在程序中調(diào)用緩沖區(qū)的輸出方法out.flush()或response.flushbuffer(),web container才將緩沖區(qū)中的數(shù)據(jù)發(fā)送給client。
這種重定向方式是利用服務(wù)器端的緩沖區(qū)機(jī)制,在把緩沖區(qū)的數(shù)據(jù)發(fā)送到客戶端之前,原來(lái)的數(shù)據(jù)不發(fā)送,將執(zhí)行轉(zhuǎn)向重定向頁(yè)面,發(fā)送重定向頁(yè)面的數(shù)據(jù),重定向調(diào)用頁(yè)的數(shù)據(jù)將被清除。如果在<JSP:FORWORD>之前有很多輸出,前面的輸出已使緩沖區(qū)滿,將自動(dòng)輸出到客戶端,那么這種重定向方式將不起作用,這一點(diǎn)應(yīng)該特別注意。
public void doPost(HttpServletRequest request,HttpServletResponse response)?? throws ServletException,IOException
{
??????? response.setContentType("text/html; charset=UTF-8");
??????? ServletContext sc = getServletContext();
??????? RequestDispatcher rd = null;
??????? rd = sc.getRequestDispatcher("/index.jsp");
??????? rd.forward(request, response);
}
二、區(qū)別.
1、forward重定向是在容器內(nèi)部實(shí)現(xiàn)的同一個(gè)Web應(yīng)用程序的重定向,所以forward方法只能重定向到同一個(gè)Web應(yīng)用程序中的一個(gè)資源,重定向后瀏覽器地址欄URL不變,而sendRedirect方法可以重定向到任何URL, 因?yàn)檫@種方法是修改http頭來(lái)實(shí)現(xiàn)的,URL沒(méi)什么限制,重定向后瀏覽器地址欄URL改變。
2、forward重定向?qū)⒃嫉腍TTP請(qǐng)求對(duì)象(request)從一個(gè)servlet實(shí)例傳遞到另一個(gè)實(shí)例,而采用sendRedirect方式兩者不是同一個(gè)application。
3、基于第二點(diǎn),參數(shù)的傳遞方式不一樣。forward的form參數(shù)跟著傳遞,所以在第二個(gè)實(shí)例中可以取得HTTP請(qǐng)求的參數(shù)。sendRedirect只能通過(guò)鏈接傳遞參數(shù),response.sendRedirect(“l(fā)ogin.jsp?param1=a”)。
4、sendRedirect能夠處理相對(duì)URL,自動(dòng)把它們轉(zhuǎn)換成絕對(duì)URL,如果地址是相對(duì)的,沒(méi)有一個(gè)‘/’,那么Web container就認(rèn)為它是相對(duì)于當(dāng)前的請(qǐng)求URI的。比如,如果為response.sendRedirect("login.jsp"),則會(huì)從當(dāng)前servlet 的URL路徑下找login.jsp: http://10.1.18.8:8081/dms/servlet/Servlet 重定向的URL: http://10.1.18.8:8081/dms/servlet/login.jsp,如果為response.sendRedirect("/login.jsp")則會(huì)從當(dāng)前應(yīng)用徑下查找url:http://10.1.18.8:8081/login.jsp。而forward不能這樣處理相對(duì)路徑。
java
他們的區(qū)別是:
response.sendRedirect是向客戶瀏覽器發(fā)送頁(yè)面重定向指令,瀏覽器接收后將向web服務(wù)器重新發(fā)送頁(yè)面請(qǐng)求,所以執(zhí)行完后瀏覽器的url顯示的是跳轉(zhuǎn)后的頁(yè)面。跳轉(zhuǎn)頁(yè)面可以是一個(gè)任意的url(本服務(wù)器的和其他服務(wù)器的均可)。
RequestDispatcher.forward則是直接在服務(wù)器中進(jìn)行處理,將處理完后的信息發(fā)送給瀏覽器進(jìn)行顯示,所以完成后在url中顯示的是跳轉(zhuǎn)前的頁(yè)面。在forward的時(shí)候?qū)⑸弦豁?yè)面中傳送的request和response信息一同發(fā)送給下一頁(yè)面(而response.sendRedirect不能將上一頁(yè)面的request和response信息發(fā)送到下一頁(yè)面)。由于forward是直接在服務(wù)器中進(jìn)行處理,所以forward的頁(yè)面只能是本服務(wù)器的。
JSP:
1) response.sendRedirect();
和servlet的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í)行,如同新開(kāi)了線程,但是對(duì)response的操作已經(jīng)無(wú)意義了;
如果要跳到相同主機(jī)下,此語(yǔ)句后面的語(yǔ)句執(zhí)行完成后才會(huì)跳轉(zhuǎn);
2) response.setHeader("Location","");
此語(yǔ)句前不允許有out.flush(),如果有,頁(yè)面不會(huì)跳轉(zhuǎn)。
跳轉(zhuǎn)后瀏覽器地址欄變化
此語(yǔ)句后面的語(yǔ)句執(zhí)行完成后才會(huì)跳轉(zhuǎn)
3) <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)
來(lái)自:http://wenku.baidu.com/link?url=Z8dLJDFT1MX6-yDqajj71B3rc32Zx79iybgSkMoqg922FApVCTviGuYhflaWUiaMYhcAc-It1pKKq2Y127JCYp41U_y4sNsVpQ8AJ_vaEvS
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/3998013.html
總結(jié)
以上是生活随笔為你收集整理的forward 和redirect的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: rsync+inotify实现服务器之间
- 下一篇: MySQL批量更新死锁案例分析--转载