日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

forward 和redirect

發(fā)布時(shí)間:2025/4/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 forward 和redirect 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. forward方法使用

request.getRequestDispatcher(path).forward(request.response);

首先來(lái)看getRequestDispatcher方法,path必須是相對(duì)路徑。

getRequestDispatcher

RequestDispatcher getRequestDispatcher(String?path)
Returns a?RequestDispatcher?object that acts as a wrapper for the resource located at the given path. A?RequestDispatcher?object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.

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.

?

Parameters:
path?- a?String?specifying the pathname to the resource. If it is relative, it must be relative against the current servlet.
Returns:
a?RequestDispatcher?object that acts as a wrapper for the resource at the specified path, or?null?if the servlet container cannot return a?RequestDispatcher
See Also:
RequestDispatcher,?ServletContext.getRequestDispatcher(java.lang.String)

接著forward方法,

forward

void forward(ServletRequest?request,ServletResponse?response)throws ServletException,IOException
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

For 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.

?

Parameters:
request?- a?ServletRequest?object that represents the request the client makes of the servlet
response?- a?ServletResponse?object that represents the response the servlet returns to the client
Throws:
ServletException?- if the target resource throws this exception
IOException?- if the target resource throws this exception
IllegalStateException?- if the response was already committed

2.sendRedirect方法使用

HttpServletResponse中使用sendRedirect可以實(shí)現(xiàn)跳轉(zhuǎn),可以接受相對(duì)路徑或者絕對(duì)路徑。

sendRedirect

public void sendRedirect(java.lang.String?location)throws java.io.IOException
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

If 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.

?

參數(shù)
定位?- the redirect location URL
Throws:
java.io.IOException?- If an input or output exception occurs
java.lang.IllegalStateException?- If the response was committed or if a partial URL is given and cannot be converted into a valid URL

Servlet 跳轉(zhuǎn) redirectforward跳轉(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 小向美奈子在线观看 | 女人叫床高潮娇喘声录音mp3 | 国产精品夜夜夜爽阿娇 | 国产男男chinese网站 | 久久久www成人免费精品 | 国产精久久久久久 | 免费三级黄色 | 久久久精品中文字幕 | 国产成人麻豆免费观看 | 精品熟妇无码av免费久久 | 成年人看的视频网站 | 成人网免费看 | 中文天堂在线资源 | 欧美日韩综合一区二区三区 | 亚洲国产精品成人va在线观看 | 欧美日韩精品在线观看 | 天堂中文网 | 黄页免费网站 | 国产不卡一 | 欧色丰满女同hd | 亚洲精品成人 | 激情免费网站 | 精品人妻一区二区三区蜜桃视频 | 欧美成人自拍视频 | 在线视频播放大全 | 成人小视频在线观看 | 亚洲最大av网 | 亚洲欧美一区二区三区四区 | 女人被狂躁60分钟视频 | 男人的天堂2019 | 成人动漫在线观看视频 | 男女草逼视频 | 中文字幕亚洲精品在线观看 | 亚洲天堂男人av | 人人玩人人干 | 嫩草国产| av无码一区二区三区 | 伊朗做爰xxxⅹ性视频 | 午夜激情毛片 | 亚洲精品综合久久 | 男女视频网站 | 天天做天天看 | 丰满少妇xoxoxo视频 | 制服丝袜国产精品 | jizz成人 | 人妻精油按摩bd高清中文字幕 | 99久久影视| 亚州av网站 | 新97超碰| 午夜精品视频 | 日韩一区二区三区在线观看视频 | 欧美群交射精内射颜射潮喷 | 日本久久久久久 | 可以免费看的黄色网址 | 新婚若妻侵犯中文字幕 | 午夜在线一区二区 | 91禁在线看 | 国产成人精品免高潮费视频 | 成人在线免费电影 | 国产精品制服诱惑 | 国产精品国产三级国产专区53 | 在线国产欧美 | 婷婷激情综合 | 色七七网站 | 老熟妇高潮一区二区三区 | 熟女一区二区三区四区 | 免费黄色小说视频 | 91人人爱 | a久久久久久 | 久久窝窝 | 奇米影视欧美 | 国产寡妇色xxⅹ交肉视频 | 鬼灭之刃柱训练篇在线观看 | 亚洲一区二区三区久久久成人动漫 | 爱欲av| 樱桃av| 国产中文字幕第一页 | 尤物视频在线看 | 午夜爽视频 | 黄色片中国 | 欧美黑人猛交 | 你懂的视频在线播放 | 超碰在| 少妇丰满尤物大尺度写真 | 国产91精品ai换脸 | 一区二区视频国产 | 97插插插 | 国产美女久久久久 | 特级免费毛片 | 激情 小说 亚洲 图片 伦 | 亚洲福利在线观看 | 女生高潮视频在线观看 | 国产性猛交╳xxx乱大交 | 黑丝少妇喷水 | www.黄色网 | 99久久免费看精品国产一区 | 人乳喂奶hd无中字 | 黄污视频网站 | 亚洲在线视频一区 |