Java中Request請(qǐng)求轉(zhuǎn)發(fā)詳解
直接來,RequestDemo5代碼,get請(qǐng)求和post請(qǐng)求都請(qǐng)求轉(zhuǎn)發(fā)了,轉(zhuǎn)發(fā)到RequestDemo6請(qǐng)求
RequestDemo5代碼
package com.lingaolu.request; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; /** * @author 林高祿 * @create 2020-07-07-12:06 */ @WebServlet("/requestDemo5") public class RequestDemo5 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("requestDemo5進(jìn)來了......post"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo6"); requestDispatcher.forward(request,response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("requestDemo5進(jìn)來了......get"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo6"); requestDispatcher.forward(request,response); } }
RequestDemo6代碼
package com.lingaolu.request; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; /** * @author 林高祿 * @create 2020-07-07-12:06 */ @WebServlet("/requestDemo6") public class RequestDemo6 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("requestDemo6進(jìn)來了......post"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("requestDemo6進(jìn)來了......get"); } }
瀏覽器訪問/requestDemo5接口
控制臺(tái)輸出
從以上結(jié)果可以看出
請(qǐng)求過后我們的瀏覽器地址還是http://localhost:8080/myRequest/requestDemo5
從瀏覽器的F12調(diào)試頁面可以看出,轉(zhuǎn)發(fā)只是一次請(qǐng)求,只有/requestDemo5請(qǐng)求,說明,可共享數(shù)據(jù)Request共享數(shù)據(jù)
我們用Postman進(jìn)行post請(qǐng)求一下
后臺(tái)輸出:
可見,get請(qǐng)求的轉(zhuǎn)發(fā)會(huì)轉(zhuǎn)發(fā)到get請(qǐng)求,post請(qǐng)求的轉(zhuǎn)發(fā),會(huì)轉(zhuǎn)發(fā)到post請(qǐng)求
我們改一下RequestDemo5的代碼,轉(zhuǎn)發(fā)到百度
package com.lingaolu.request; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; /** * @author 林高祿 * @create 2020-07-07-12:06 */ @WebServlet("/requestDemo5") public class RequestDemo5 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("requestDemo5進(jìn)來了......post"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo6"); requestDispatcher.forward(request,response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("requestDemo5進(jìn)來了......get"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("https://www.baidu.com/"); requestDispatcher.forward(request,response); } }
瀏覽器請(qǐng)求
后臺(tái)輸出:
從后臺(tái)輸出看出,requestDemo5請(qǐng)求進(jìn)來了,從瀏覽器看出,轉(zhuǎn)發(fā)失敗了,而且從實(shí)際轉(zhuǎn)發(fā)的路徑上看,因?yàn)樘摂M路徑,所以請(qǐng)求轉(zhuǎn)發(fā)只能轉(zhuǎn)發(fā)到當(dāng)前服務(wù)器內(nèi)部的資源
請(qǐng)求轉(zhuǎn)發(fā)的特點(diǎn)總結(jié):(與之對(duì)應(yīng)的------重定向的詳情與特點(diǎn))
- 瀏覽器地址欄路徑不發(fā)生變化
- 轉(zhuǎn)發(fā)只是一次請(qǐng)求,可共享數(shù)據(jù)Request共享數(shù)據(jù)
- 哪種請(qǐng)求方式只能轉(zhuǎn)發(fā)到那種請(qǐng)求方式
- 請(qǐng)求轉(zhuǎn)發(fā)只能轉(zhuǎn)發(fā)到當(dāng)前服務(wù)器內(nèi)部的資源
到此這篇關(guān)于Java中Request請(qǐng)求轉(zhuǎn)發(fā)詳解的文章就介紹到這了,更多相關(guān)Java Request請(qǐng)求轉(zhuǎn)發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中JsonObject與JsonArray轉(zhuǎn)換方法實(shí)例
在項(xiàng)目日常開發(fā)中常常會(huì)遇到JSONArray和JSONObject的轉(zhuǎn)換,很多公司剛?cè)肼毜男∶刃聲?huì)卡在這里,下面這篇文章主要給大家介紹了關(guān)于java中JsonObject與JsonArray轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2023-04-04MapStruct實(shí)體轉(zhuǎn)換及List轉(zhuǎn)換的方法講解
今天小編就為大家分享一篇關(guān)于MapStruct實(shí)體轉(zhuǎn)換及List轉(zhuǎn)換的方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Java 獲取Html文本中的img標(biāo)簽下src中的內(nèi)容方法
今天小編就為大家分享一篇Java 獲取Html文本中的img標(biāo)簽下src中的內(nèi)容方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06

通過Spring Boot + Mybatis + Redis快速搭建現(xiàn)代化Web項(xiàng)目

面向切面的Spring通過切點(diǎn)來選擇連接點(diǎn)實(shí)例詳解

java實(shí)現(xiàn)讀取txt文件中的內(nèi)容

Java中隊(duì)列Queue和Deque的區(qū)別與代碼實(shí)例