Java中Request请求转发详解

时间:2021-05-20

直接来,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:///"); requestDispatcher.forward(request,response); }}

浏览器请求

后台输出:

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

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

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

到此这篇关于Java中Request请求转发详解的文章就介绍到这了,更多相关Java Request请求转发内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章