当前位置:网站首页>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 ?

边栏推荐
猜你喜欢

Ipaylinks, a cross-border payment integration service, won the 3A Asia Award of treasury

SV强制类型转换和常数

Can Lu Zhengyao hide from the live broadcast room dominated by Luo min?

Review and analysis of noodle dishes

Huawei machine test - topic core test point

Colleges and universities have introduced 23 Philippine doctors with heavy funds, and the relevant departments have launched an investigation!

Opencv picture rotation

Nearly 30 colleges and universities were named and praised by the Ministry of education!

NPM install reported -4058 error

Internship report 1 - face 3D reconstruction method
随机推荐
A problem of MySQL database
Pay close attention! List of the latest agenda of 2022 open atom open source Summit
The use and Simulation of character and string library functions in C language
Tensorflow introductory tutorial (38) -- V2 net
Are the top ten securities companies safe and risky to open accounts?
EF core data filtering
Use prometheus+grafana to monitor MySQL performance indicators
分家后印象笔记过日子依然不好过,骚操作却不少
Shardingsphere database read / write separation
Opencv picture rotation
MySQL数据库的一个问题
Df2net 3D model deployment
C language to achieve a static version of the address book
Mobile robot (IV) four axis aircraft
Review and analysis of noodle dishes
700. 二叉搜索树中的搜索-dfs法
UFW port forwarding
Eth POS 2.0 stacking test network pledge process
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo


