当前位置:网站首页>Interview assault 66: what is the difference between request forwarding and request redirection?
Interview assault 66: what is the difference between request forwarding and request redirection?
2022-07-24 17:44:00 【JAVA Chinese community】

author | Lei brother
source | Java Analysis of the real interview questions (ID:aimianshi666)
Please contact authorization for reprint ( WeChat ID:GG_Stone)
stay Java in , There are two ways to realize jump : Request forwarding and request redirection , But the two are completely different , So let's play it today . The main difference between request forwarding and request redirection , Contains the following 5 spot :
Different definitions
Jump party is different
Data sharing is different
Final URL Address different
The code implementation is different
Next , Let's look at them one by one .
1. Different definitions
Request forwarding (Forward): Occurs inside the server program , When the server receives a request from a client , Will first request , Forward to the destination address , Then forward the result returned by the target address to the client . And the client has no perception of all this , This is like comparison. , Zhang San ( client ) Look for Li Si ( Server side ) To borrow money , And Li Si has no money , So Li Si went to Wang Wu to borrow money , And lent the money to Zhang San , During the whole process, Zhang San only borrowed money once , The rest of the work was done by Li Si , This is request forwarding . request redirections (Redirect): Request redirection refers to after the server receives the request from the client , Will return a temporary response header to the client , This temporary response header records , The client needs to send the request again ( Redirect ) Of URL Address , After the client receives the address , The request will be sent to the new address , This is request redirection . It's like Zhang San ( client ) Look for Li Si ( Server side ) To borrow money , Li Si has no money , So Li Si told Zhang San ,“ I have no money , Go to Wang Wu to borrow “, So Zhang San went to Wang Wu's house to borrow money , This is request redirection .
2. The requestor is different
From the above definition of request forwarding and request redirection , We can see that : Request forwarding is a server-side behavior , The server sends the request instead of the client , And return the result to the client ; And requesting redirection is the behavior of the client , Their interaction process , As shown in the figure below :
3. Data sharing is different
Request forwarding is implemented on the server , So in the whole execution process , client ( Browser side ) Just send the request once , Therefore, the same one is used in the whole interaction process Request Request object and a Response The response object , So the whole request process , The requested and returned data are shared ; The request redirection client sends two completely different requests , So the data in the two requests are different .
4. Final URL Address different
Request forwarding is the server-side request , And then return the result to the client , So the whole request process URL The address is the same ; The request for redirection is for the server to tell the client ,“ You go to another place to visit ”, So the browser will send the request again , So the client end up showing URL Also the address of the final jump , Instead of the address you just requested , therefore URL The address has changed .
5. The code implementation is different
stay SpringBoot in , The implementation code of request forwarding is as follows :
@RequestMapping("/fw")
public void forward(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/index.html").forward(request, response);
}The implementation code of request redirection is as follows :
@RequestMapping("/rt")
public void redirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendRedirect("/index.html");
}summary
Request forwarding and request redirection follow “ The less words , The bigger the matter is ” Principles ,“ Request forwarding ” There are fewer words , It needs to perform jump on behalf of the client ; and “ request redirections ” A lot of words , It does nothing , Just tell the client “ You visit somewhere else ” That's it , This is the key to understand these two concepts . Their differences are mainly reflected in 5 A little bit : Different definitions 、 The requestor is different 、 Data sharing is different 、 Final URL Address different 、 The code implementation is different .
Reference resources & Acknowledgement
www.jianshu.com/p/81407dff803d
www.cnblogs.com/su-chu-zhi-151/p/11875641.html
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number .
official account :Java Analysis of the real interview questions
Interview collection :https://gitee.com/mydb/interview

Previous recommendation Interview shock 65: Why use HTTPS? What is it good for ?
Interview shock 64: understand HTTP Agreement ?
Interview shock 63:MySQL How to remove weight in ?

边栏推荐
- ShardingSphere数据库读写分离
- mac数据库管理软件Navicat Premium Essentials Mac
- The use and Simulation of character and string library functions in C language
- Review and analysis of noodle dishes
- Two dimensional convolution -- use of torch.nn.conv2d
- Getaverse, a distant bridge to Web3
- Common questions of testers during interview
- Class bytecode file
- Getaverse,走向Web3的远方桥梁
- wallys/IPQ8074A 4x4 2.4G 8x8 5G 802.11ax
猜你喜欢

hcip第三天

ansible自动化运维详解(五)ansible中变量的设定使用、JINJA2模板的使用以及ansible的加密控制

Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection

Two dimensional convolution -- use of torch.nn.conv2d

C语言自定义类型 — 枚举

Step by step introduction to the development framework based on sqlsugar (12) -- split the content of the page module into components to realize the division and rule processing

Logical operation of image pixels

Tensorflow introductory tutorial (40) -- acunet

干货|值得收藏的三款子域名收集工具

二维卷积——torch.nn.conv2d的使用
随机推荐
Tensorflow introductory tutorial (40) -- acunet
再见收费的Navicat!这款开源的数据库管理工具界面更炫酷!
Heuristic merging (including examples of general formula and tree heuristic merging)
C语言自定义类型讲解 — 联合体
DHCP relay of HCNP Routing & Switching
C language to achieve a static version of the address book
JS & TS learning summary
C语言中的字符与字符串库函数的使用以及模拟实现
hcip第三天
Step by step introduction to the development framework based on sqlsugar (12) -- split the content of the page module into components to realize the division and rule processing
Use prometheus+grafana to monitor MySQL performance indicators
[spoken English] 01 - Introduction to atom
Use yarn
Shardingsphere database read / write separation
ShardingSphere数据库读写分离
Open source Invoicing system, 10 minutes to complete, it is recommended to collect!
ROC and AUC details of the recommended system
Two dimensional convolution -- use of torch.nn.conv2d
ShardingSphere数据库读写分离
Preliminary study of Oracle pl/sql


