forward 和redirect
1. forward方法使用
request.getRequestDispatcher(path).forward(request.response);首先來看getRequestDispatcher方法,path必須是相對路徑。
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可以實現跳轉,可以接受相對路徑或者絕對路徑。
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 跳轉 redirect與forward跳轉的區別
Servlet:
當然,在servlet中,一般跳轉都發生在doGet, doPost等方法里面。
一、原理
1) redirect 方式
response.sendRedirect("/a.jsp");
頁面的路徑是相對路徑。sendRedirect可以將頁面跳轉到任何頁面,不一定局限于本web應用中,如:
response.sendRedirect("http://www.ycul.com");
跳轉后瀏覽器地址欄變化。
這種方式要傳值出去的話,只能在url中帶parameter或者放在session中,無法使用request.setAttribute來傳遞。
這種方式是在客戶端作的重定向處理。該方法通過修改HTTP協議的HEADER部分,對瀏覽器下達重定向指令的,讓瀏覽器對在location中指定的URL提出請求,使瀏覽器顯示重定向網頁的內容。該方法可以接受絕對的或相對的URLs。如果傳遞到該方法的參數是一個相對的URL,那么Web container在將它發送到客戶端前會把它轉換成一個絕對的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);
頁面的路徑是相對路徑。forward方式只能跳轉到本web應用中的頁面上。
跳轉后瀏覽器地址欄不會變化。
使用這種方式跳轉,傳值可以使用三種方法:url中帶parameter,session,request.setAttribute
這種方式是在服務器端作的重定向。服務器往client發送數據的過程是這樣的:服務器在向客戶端發送數據之前,是先將數據輸出到緩沖區,然后將緩沖區中數據發送給client端。什么時候將緩沖區里的數據發送給client端呢?(1)當對來自client的request處理完,并把所有數據輸出到緩沖區,(2)當緩沖區滿,(3)在程序中調用緩沖區的輸出方法out.flush()或response.flushbuffer(),web container才將緩沖區中的數據發送給client。
這種重定向方式是利用服務器端的緩沖區機制,在把緩沖區的數據發送到客戶端之前,原來的數據不發送,將執行轉向重定向頁面,發送重定向頁面的數據,重定向調用頁的數據將被清除。如果在<JSP:FORWORD>之前有很多輸出,前面的輸出已使緩沖區滿,將自動輸出到客戶端,那么這種重定向方式將不起作用,這一點應該特別注意。
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);
}
二、區別.
1、forward重定向是在容器內部實現的同一個Web應用程序的重定向,所以forward方法只能重定向到同一個Web應用程序中的一個資源,重定向后瀏覽器地址欄URL不變,而sendRedirect方法可以重定向到任何URL, 因為這種方法是修改http頭來實現的,URL沒什么限制,重定向后瀏覽器地址欄URL改變。
2、forward重定向將原始的HTTP請求對象(request)從一個servlet實例傳遞到另一個實例,而采用sendRedirect方式兩者不是同一個application。
3、基于第二點,參數的傳遞方式不一樣。forward的form參數跟著傳遞,所以在第二個實例中可以取得HTTP請求的參數。sendRedirect只能通過鏈接傳遞參數,response.sendRedirect(“login.jsp?param1=a”)。
4、sendRedirect能夠處理相對URL,自動把它們轉換成絕對URL,如果地址是相對的,沒有一個‘/’,那么Web container就認為它是相對于當前的請求URI的。比如,如果為response.sendRedirect("login.jsp"),則會從當前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")則會從當前應用徑下查找url:http://10.1.18.8:8081/login.jsp。而forward不能這樣處理相對路徑。
java
他們的區別是:
response.sendRedirect是向客戶瀏覽器發送頁面重定向指令,瀏覽器接收后將向web服務器重新發送頁面請求,所以執行完后瀏覽器的url顯示的是跳轉后的頁面。跳轉頁面可以是一個任意的url(本服務器的和其他服務器的均可)。
RequestDispatcher.forward則是直接在服務器中進行處理,將處理完后的信息發送給瀏覽器進行顯示,所以完成后在url中顯示的是跳轉前的頁面。在forward的時候將上一頁面中傳送的request和response信息一同發送給下一頁面(而response.sendRedirect不能將上一頁面的request和response信息發送到下一頁面)。由于forward是直接在服務器中進行處理,所以forward的頁面只能是本服務器的。
JSP:
1) response.sendRedirect();
和servlet的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的操作已經無意義了;
如果要跳到相同主機下,此語句后面的語句執行完成后才會跳轉;
2) response.setHeader("Location","");
此語句前不允許有out.flush(),如果有,頁面不會跳轉。
跳轉后瀏覽器地址欄變化
此語句后面的語句執行完成后才會跳轉
3) <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)
...
跳轉后瀏覽器地址欄不變,但是只能跳到當前主機下
此語句后面的語句執行完成后才會跳轉
來自:http://wenku.baidu.com/link?url=Z8dLJDFT1MX6-yDqajj71B3rc32Zx79iybgSkMoqg922FApVCTviGuYhflaWUiaMYhcAc-It1pKKq2Y127JCYp41U_y4sNsVpQ8AJ_vaEvS
轉載于:https://www.cnblogs.com/davidwang456/p/3998013.html
總結
以上是生活随笔為你收集整理的forward 和redirect的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rsync+inotify实现服务器之间
- 下一篇: MySQL批量更新死锁案例分析--转载