欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中如何使用Response重定向

 更新時(shí)間:2020年07月12日 11:55:51   作者:持續(xù)更新,2天一篇  
這篇文章主要介紹了Java中如何使用Response重定向,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

直接來,ResponseDemo1代碼,get請(qǐng)求到post請(qǐng)求,重定向到ResponseDemo2

我的虛擬目錄為

ResponseDemo1代碼,302是重定向狀態(tài)碼,Http狀態(tài)碼大全

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高祿
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo1")
public class ResponseDemo1 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo1進(jìn)來了......");
  // 設(shè)置狀態(tài)碼為302
  response.setStatus(302);
  // 動(dòng)態(tài)獲取虛擬目錄,以后修改虛擬目錄的時(shí)候就不用了修改代碼
  String contextPath = request.getContextPath();
  // 設(shè)置響應(yīng)頭location
  response.setHeader("location",contextPath+"/responseDemo2");
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

ResponseDemo2代碼

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高祿
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo2")
public class ResponseDemo2 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo2進(jìn)來了......");
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

瀏覽器訪問,訪問前

瀏覽器訪問,訪問后

控制臺(tái)輸出

從以上結(jié)果可以看出:

  • 瀏覽器上的請(qǐng)求地址變了
  • 重定向是2次請(qǐng)求,一次是responseDemo1請(qǐng)求,一次是responseDemo2請(qǐng)求

由于狀態(tài)碼固定是302,響應(yīng)頭固定是location,所以請(qǐng)求轉(zhuǎn)發(fā)提供了更方便的方式

void sendRedirect(String var1)

ResponseDemo3代碼

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高祿
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo3進(jìn)來了......");;
  // 動(dòng)態(tài)獲取虛擬目錄,以后修改虛擬目錄的時(shí)候就不用了修改代碼
  String contextPath = request.getContextPath();
  response.sendRedirect(contextPath+"/responseDemo2");
 
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

啟動(dòng),瀏覽器訪問

控制臺(tái)輸出

我們把ResponseDemo3代碼改一下,重定向到百度

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高祿
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo3進(jìn)來了......");;
  response.sendRedirect("http://www.baidu.com");
 
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

重啟訪問,就重定向到百度了

從上面結(jié)果我們可以看出,重定向可以訪問其他服務(wù)器的資源

我們寫一個(gè)ResponseDemo4,探索一下共享數(shù)據(jù)情況,ResponseDemo4里請(qǐng)求寫進(jìn)了一個(gè)數(shù)據(jù)name

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高祿
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo4進(jìn)來了......");
  request.setAttribute("name","林大帥");
  String contextPath = request.getContextPath();
  response.sendRedirect(contextPath+"/responseDemo2");
 
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

同時(shí)ResponseDemo2改一下,獲取數(shù)據(jù)name

重啟,瀏覽器訪問

控制臺(tái)輸出:

從上面的null可以看出,重定向不可以共享數(shù)據(jù)

重定向的特點(diǎn)總結(jié):(與之對(duì)應(yīng)的------請(qǐng)求轉(zhuǎn)發(fā)詳情與特點(diǎn)

  • 瀏覽器地址欄路徑發(fā)生變化,最終地址為最后轉(zhuǎn)發(fā)的地址
  • 轉(zhuǎn)發(fā)只是多次請(qǐng)求,轉(zhuǎn)發(fā)幾次就幾次請(qǐng)求,不共享數(shù)據(jù)
  • 可以訪問其他服務(wù)器的資源

到此這篇關(guān)于Java中如何使用Response重定向的文章就介紹到這了,更多相關(guān)Java使用Response重定向內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 2023年IDEA最新永久激活教程(親測(cè)可用)

    2023年IDEA最新永久激活教程(親測(cè)可用)

    打開電腦,發(fā)現(xiàn)?IDEA?又更新了一個(gè)小版本,2022.3.2?版本來了,真的是非常高興,那么這么新的版本怎么激活使用呢?下面小編給大家?guī)砹薸dea2023年最新永久激活方法,感興趣的朋友一起看看吧
    2023-04-04
  • SpringBoot部署在tomcat容器中運(yùn)行的部署方法

    SpringBoot部署在tomcat容器中運(yùn)行的部署方法

    這篇文章主要介紹了SpringBoot部署在tomcat容器中運(yùn)行的部署方法,需要的朋友可以參考下
    2018-10-10
  • 詳解java模板和回調(diào)機(jī)制

    詳解java模板和回調(diào)機(jī)制

    這篇文章主要為大家詳細(xì)介紹了java模板和回調(diào)機(jī)制,學(xué)習(xí)java模板,感興趣的朋友可以參考一下
    2016-03-03
  • Spring ApplicationListener的使用詳解

    Spring ApplicationListener的使用詳解

    這篇文章主要介紹了Spring ApplicationListener的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Springboot整合RabbitMq測(cè)試TTL的方法詳解

    Springboot整合RabbitMq測(cè)試TTL的方法詳解

    這篇文章主要介紹了Springboot整合RabbitMq測(cè)試TTL的設(shè)置,設(shè)置TTL一般由兩種設(shè)置方法,設(shè)置整個(gè)隊(duì)列的過期時(shí)間另一種設(shè)置單個(gè)消息的過期時(shí)間,通過示例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Hadoop運(yùn)行時(shí)遇到j(luò)ava.io.FileNotFoundException錯(cuò)誤的解決方法

    Hadoop運(yùn)行時(shí)遇到j(luò)ava.io.FileNotFoundException錯(cuò)誤的解決方法

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著Hadoop運(yùn)行時(shí)遇到j(luò)ava.io.FileNotFoundException錯(cuò)誤展開,文中有非常詳細(xì)的解決方法,需要的朋友可以參考下
    2021-06-06
  • java中hashCode方法與equals方法的用法總結(jié)

    java中hashCode方法與equals方法的用法總結(jié)

    總的來說,Java中的集合(Collection)有兩類,一類是List,再有一類是Set。前者集合內(nèi)的元素是有序的,元素可以重復(fù);后者元素?zé)o序,但元素不可重復(fù)
    2013-10-10
  • java &與&&的區(qū)別及實(shí)例

    java &與&&的區(qū)別及實(shí)例

    這篇文章主要介紹了java &與&&的區(qū)別的相關(guān)資料,并附簡(jiǎn)單實(shí)例,幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下
    2016-10-10
  • 詳解Java中NullPointerException異常的原因和解決辦法

    詳解Java中NullPointerException異常的原因和解決辦法

    本文主要介紹了詳解Java中NullPointerException異常的原因和解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 基于OpenCV與JVM實(shí)現(xiàn)矩陣處理圖像

    基于OpenCV與JVM實(shí)現(xiàn)矩陣處理圖像

    本文主要介紹了Java圖像處理實(shí)戰(zhàn)之基于OpenCV與JVM實(shí)現(xiàn)矩陣處理圖像。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)圖像處理有一定的幫助,感興趣的可以試一試
    2022-01-01

最新評(píng)論