Java中Request请求转发详解

所属分类: 软件编程 / java 阅读数: 69
收藏 0 赞 0 分享

直接来,RequestDemo5代码,get请求和post请求都请求转发了,转发到RequestDemo6请求

 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进来了......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进来了......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进来了......post");
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 System.out.println("requestDemo6进来了......get");
 }
}

浏览器访问/requestDemo5接口

控制台输出

从以上结果可以看出

请求过后我们的浏览器地址还是http://localhost:8080/myRequest/requestDemo5

从浏览器的F12调试页面可以看出,转发只是一次请求,只有/requestDemo5请求,说明,可共享数据Request共享数据

我们用Postman进行post请求一下

后台输出:

可见,get请求的转发会转发到get请求,post请求的转发,会转发到post请求

我们改一下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进来了......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进来了......get");
 RequestDispatcher requestDispatcher = request.getRequestDispatcher("https://www.baidu.com/");
 requestDispatcher.forward(request,response);
 
 }
}

浏览器请求

 后台输出:

从后台输出看出,requestDemo5请求进来了,从浏览器看出,转发失败了,而且从实际转发的路径上看,因为虚拟路径,所以请求转发只能转发到当前服务器内部的资源

请求转发的特点总结:(与之对应的------重定向的详情与特点

  • 浏览器地址栏路径不发生变化
  • 转发只是一次请求,可共享数据Request共享数据
  • 哪种请求方式只能转发到那种请求方式
  • 请求转发只能转发到当前服务器内部的资源
更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多