Java中Request请求转发详解
作者:持续更新,2天一篇 时间:2021-05-25 20:53:42
直接来,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共享数据
哪种请求方式只能转发到那种请求方式
请求转发只能转发到当前服务器内部的资源
来源:https://blog.csdn.net/lgl782519197/article/details/107183540
标签:Java,Request,请求转发
0
投稿
猜你喜欢
Android仿Iphone屏幕底部弹出半透明PopupWindow效果
2023-08-17 06:14:43
mybatis的插件机制示例详解
2023-02-24 23:46:17
Java数据结构专题解析之栈和队列的实现
2022-11-20 01:24:21
Mybatis步骤分解实现一个增删改查程序
2021-09-16 01:12:43
android主线程和子线程之间消息传递详解
2021-11-06 05:06:23
Android双击返回键退出程序的实现方法
2022-06-01 01:28:28
Yml转properties文件工具类YmlUtils的详细过程(不用引任何插件和依赖)
2021-08-18 03:55:00
Android的HTTP类库Volley入门学习教程
2022-06-08 11:56:51
详解java关于对象的比较
2022-12-11 01:28:55
C# menuStrip控件实现鼠标滑过自动弹出功能
2022-09-01 05:18:37
Android开发之Android.mk模板的实例详解
2022-02-20 09:11:44
Android使用AndroidUtilCode实现多语言
2023-08-28 15:25:50
.Net WInform开发笔记(五)关于事件Event
2023-11-22 01:00:18
JAVA中ListIterator和Iterator详解与辨析(推荐)
2021-06-11 16:47:19
c#创建浮动工具栏功能示例
2022-08-31 01:08:31
Unity中EventTrigger的几种使用操作
2022-01-15 06:54:37
详细理解JAVA面向对象的封装,继承,多态,抽象
2023-07-05 06:54:14
springboot 整合hbase的示例代码
2023-02-11 19:35:20
Android启动优化之延时加载的步骤详解
2023-07-10 20:43:41
spring boot如何实现切割分片上传
2023-06-29 02:49:26