Spring Boot 请求转发和重定向
Spring Boot Java HTTP 评论 1 大约 853 字区别
转发
- 由服务端完成;
- 地址栏不发生变化,显示的转发前的地址(针对浏览器);
- 只发生一次请求;
- 请求中携带的参数不会丢失;
重定向
- 服务端告诉浏览器或客户端重定向的地址,由客户端再次发起请求;
- 地址栏显示重新向后的地址(针对浏览器);
- 发生了两次请求;
- 第一次请求的参数不会带到重定向后的请求中;
请求链路
转发
- 浏览器 请求 服务器
- 服务器 转发 服务器2
- 服务器2 响应 浏览器
重定向
- 浏览器 请求 服务器
- 服务器 响应 浏览器
- 浏览器 请求 服务器2
- 服务器2 响应 浏览器
代码
转发
@GetMapping("/test-forward")
public void testForward(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.getRequestDispatcher("https://www.baidu.com").forward(request, response); // 无法跨域 forward
request.getRequestDispatcher("/test").forward(request, response); // 只能转发至内部 servlet
}
@GetMapping("/test")
@ResponseBody
public String test() {
return "来自test的结果";
}
重定向
@GetMapping("/test-redirect")
public void testRedirect(HttpServletResponse response) throws IOException {
response.sendRedirect("https://www.baidu.com");
}
阅读 10505 · 发布于 2020-03-28
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
-
sss 1楼
request.getRequestDispatcher("https://www.baidu.com").forward(request, response); 只可以访问同一个服务,跨不服不可以
Chrome | Windows 10 2021-04-06
随便看看
换一批
-
SonarQube Lombok Malicious code error阅读 875
-
JavaScript 生成微信二维码阅读 2848
-
软考-系统架构设计师:Cache - 高速缓存阅读 2001
-
Spring Boot 启动时自动创建数据库阅读 627
-
HTML iframe 设置不显示滚动条和边框阅读 4031
-
使用 trivy 扫描 Docker 镜像、K8S 集群的安全漏洞阅读 1447
-
Linux 网络状态工具 ss,代替 netstat阅读 14254
-
Windows 查看文件占用的几种方法阅读 5970
-
Windows 子系统设置默认 root 用户登录阅读 1137
-
Go 占用虚拟内存过高阅读 5041