当前位置:网站首页>转发和重定向的区别及使用场景
转发和重定向的区别及使用场景
2022-07-30 00:13:00 【小何┌】
目录
本质区别
转发 :是由服务器发起的,转发前后是同一次请求,共用同一个请求域(request域)。
浏览器向服务器请求一次。
重定向 :是由浏览器发起的,重定向前后是两次请求,各用各的请求域。
浏览器向服务器请求两次。

转发前后的地址不变,请求前是 http://localhost:8080/a,请求后也是 http://localhost:8080/a。
重定向前后地址变化,请求前是 http://localhost:8080/a,请求后是 http://localhost:8080/xxx。
代码区别
转发和重定向代码上的区别 :
// 转发 :
// request :请求,dispatcher :调度器
// requestDispatcher :请求调度器。
request.getRequestDispatcher("/login").forward(request, response);
// 重定向 :
response.sendRedirect("/项目名/homepage");可以看到,转发是使用request进行的,在forward()中将请求前的request和response给请求后继续使用,这也就是为什么转发是同一次请求,并且转发可以携带数据 。
重定向是使用response进行的,简单的sendRedirect()就可以完成。
值得一提的是,为什么转发中的地址不需要加项目名,重定向却需要呢?
我们知道,前端向后端发送请求就需要带项目名。
转发是由后端服务器处理的,并没有前端的事,所以不需要加项目名。
重定向是浏览器再次发送请求,属于前端发送请求,所以需要加项目名。
由于转发前后共用请求域(request域),所以转发可以携带数据,将数据存放在请求域中,request.setAttribute(key, value),在转发后可以使用requestgetAttribute(key, value)取出数据。
重定向前后不是同义词请求,但是也可以携带数据。只需要在地址中携带就可以 :
/StudentSystem/homepage?id=123&name=xiaoming
// 想要取出数据 :
request.getParameter("id");
request.getParameter("name");两者携带数据以及取出数据的方式要记清。
应用时机
以登陆举例 :
在前端输入id、password等数据后要传到后端,我们需要查询数据库看有没有这个用户。
如果成功,要进入这个人的主页。
如果失败,要返回 登陆界面并且要保留用户刚才输入的信息。
这样的功能该怎样实现呢?
用户登陆成功,之后的页面有很多都需要用户的信息,明显request域不够用,我们就将用户信息存入session域,这是已经不需要携带数据了,我们就使用重定向将页面跳转到用户首页就行了。
用户登陆失败,我们需要跳转回登陆界面,还需要携带用户刚才输入的信息,由于这个信息不需要长久保存,我们就将其存入request域,转发到登录界面。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String id = request.getParameter("id");
String password = request.getParameter("password");
User user = UserDao.selectUser(id, password);
if(user != null) {
// 登陆成功,将用户存入session域以便日后使用
request.getSession().setAttribute("user",user);
// 用户信息已经存入session
// 直接使用重定向进入用户主页
response.sendRedirect("/StudentSystem/home");
} else if(user != null) {
// 登陆失败,跳回登陆界面
// 同时需要携带用户刚刚输入的信息
request.setAttribute("user",user);
request.getRequestDispatcher("/login").forward(request, response);
}
}边栏推荐
- Worthington弹性蛋白酶&透明质酸酶简介
- Codeforces Round #805 (Div. 3)总结
- Elephant Swap: Provide arbitrage space in the crypto market with ePLATO
- 字符串替换空格
- rk-boot框架实战(1)
- 4 hotspot inquiry networks necessary for new media operations
- EA&UML日拱一卒-多任务编程超入门-(8)多任务安全的数据类
- EA&UML日拱一卒-状态图::重画按钮状态图
- NumPy(一)
- Since the media how to write a short video title?Three hot style title, let your video gain more traffic
猜你喜欢

绘制几何图形

二维数组的查找

Toutiao We-Media Operation: How to Gain 500+ Fans in Toutiao Today?

账号权重怎么提升?自媒体运营的3个方法,帮你获得更多收益

18 Lectures on Disassembly of Multi-merchant Mall System Functions

Reading notes. This is the psychology: see through the essence of the pseudo psychology (version 10)"

EA&UML日拱一卒-多任务编程超入门-(9)线程同步

自媒体人如何打造出爆文?这3种类型的文章最容易爆

News text classification

Worthington Papain & Chymotrypsin & DNase I
随机推荐
Elephant Swap: Provide arbitrage space in the crypto market with ePLATO
rk-boot framework combat (1)
【分层强化学习】HAC源码解读
Chinese semantic matching
Filebeat如何保证在日志文件被切割(或滚动rolling)时依然正确读取文件
Introduction to Worthington Elastase & Hyaluronidase
对数据库进行增删改查操作
Genesis与Axis Ventures互动密切
Worthington解离酶:胶原酶及四个基本概况
How to design and implement report collaboration system for instruction set data products——Development practice of industrial collaborative manufacturing project based on instruction set IoT operating
EA & UML Sun Arch - State Diagram :: Redraw Button State Diagram
Docker install MySQL8.0
绘制几何图形
账号权重怎么提升?自媒体运营的3个方法,帮你获得更多收益
"The lighthouse factory" of China path: smart roll out from point to surface
重写并自定义依赖的原生的Bean方法
YOLO数据格式说明与转换
ZLMediaKit源码分析 - WebRtc连接迁移
Worthington Papain & Chymotrypsin & DNase I
Reading notes. This is the psychology: see through the essence of the pseudo psychology (version 10)"