8--Request forwarding and redirection

How request forwarding works

Method 1: The
browser sends a request to the Web server, and the Web server sends the obtained information to the Web container. The Web container finds the first resource (mostly pages) according to the request, because the first resource cannot complete all requests. Demand, the request is transferred from the first request to the second resource for processing, and so on. When all resources have processed the request, the last resource will provide the processing result to the Web container, and the Wed container will send the processing result to the Web service, and the server will send the final result to the client (browser) based on this principle. Also called the temporary transfer of rights. As shown in the figure:
Insert picture description here
Method 2: The
browser sends a request to the Web server, and the Web server sends the obtained information to the Web container. The Web container finds the first resource (mostly pages) according to the request, because the first resource cannot Complete all request requirements, the request is transferred from the first request to the second resource for processing, and so on. After the last resource is processed, the processing result is passed to the previous resource in the reverse direction. Finally, the first resource will send the processing result to the Web container. The Wed container will hand the processing result to the Web service, and the server will send the final result. Corresponding to the client (browser), based on this principle is also called the temporary transfer of rights. As shown in the figure:
Insert picture description here
Example 1: Request range to store data

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		//向request范围中添加属性
		request.setAttribute("info", "这是request范围中的数据");
	    request.setAttribute("username", "张三123");
	%>
	<!-- 请求转发 -->
	<jsp:forward page="demo03.jsp"></jsp:forward>	
</body>
</html>

Example 2: Receive data in the request range

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 请求转发(服务端跳转)能获取request范围中的值 
	         请求转发:客户端端只发起过一次HTTP请求,如果访问的一个资源无法完成整个请求回应,则在服务器端
	     	 进行跳转,跳转之后请求中的数据不会丢失,只有所有的资源共同完成请求后,再相应。所以一次请求中
	     	 可以获取request范围中的数据
	 -->
	info:<%=request.getAttribute("info") %><br/>
	username:<%=request.getAttribute("username") %><br/>
</body>
</html>

Operation result:
info: this is the data in the request range
username: Zhang San123

How Redirection Works

Redirection (the Redirect) by various methods that various network requests to re- set a direction to other locations (eg: redirecting page redirection, the domain name, but also to change the routing of data packets via a path weight Orientation). As shown in the figure:
Insert picture description here
Example 3: Data is stored in the request range

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		//向request范围中添加属性
		request.setAttribute("info", "这是request范围中的数据");
	    request.setAttribute("username", "张三123");
	%>
	<!-- a标签跳转属于重定向 -->
	<a href="demo03.jsp">跳转到demo03</a>
</body>
</html>

Example 4: Receive data in the request range

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!--重定向(客户端的跳转),不能获取request范围中的值
	  重定向:客户端发起http请求后,如果服务端的一个资源无法完成整个请求回应,则需要客户端再次发起请求(相当于多次请求)
	       在发起请求后可能之前的数据不存在了,所以重定向后数据丢失-->
	info:<%=request.getAttribute("info") %><br/>
	username:<%=request.getAttribute("username") %><br/>
</body>
</html>

Operation result:
info:null
username:null

The difference between request forwarding and redirection:

Request forwarding:

  1. It is the redirection of control in the container. The redirected address will not be displayed in the address bar of the client browser; the server is forwarded internally, and the entire process is in the same request.
  2. It can be understood that it is server-side behavior. The client initiates a request. This request can be passed multiple times on the entire server side, but it is all passed by the server-side processing program to another processing program. The client does not need to initiate a second request, regardless of How many processing procedures this request has gone through is always the same request, which means that every processing procedure that the data in this request has gone through can be used. The data in the request range will not be lost in the same request.
    Redirect:
  3. When redirecting the page is used, the redirection path should be displayed in the client path bar. The client can observe the change of the page address. The redirection is when the client has made at least two access requests. .
  4. It can be understood as a client behavior. The client initiates a request and the server gives a response, but this response contains the address of the server-side handler that the client needs to access next time. If the client initiates the request again, the processing result will be obtained. , Which means that the redirect client initiates at least two requests. The data in the request range is lost during the two requests.

Application scenario

  1. The way the front-end sends the request to the back-end (redirection):
      1) The browser address input link.
      2) a tag <a href="URL address"> jump< /a>
      3) form form <form action="URL address" method="get/post">
                <input type="submit" value=" jump Turn"/>
             </form>
      4) Operate window.location.href="http://www.baidu.com" through the BOM of js;
      5) AJAX asynchronous request.
  2. The use of back-end request forwarding and redirection:
      1) Request forwarding:
      req.getRequestDispatcher (request path of servlet or jsp).forward(req,resp);
      2) URL redirection:
      resp.sendRedirect("/day47/views/ student.jsp");
      
      =========================================== ================================================== ======
      I have only recently started writing articles, and I saw that everyone shared their experience with others. I am also one of the beneficiaries. I can’t just ask for what I know without giving back, so I also share the knowledge I have learned with everyone. I cannot guarantee that every question is correct, after all, I am also a learner. If there is something wrong or misunderstood, I hope you can point it out. I am very grateful. The writing process is not easy, and I hope everyone will like it and pay attention to me. It also motivates me to continue sharing.
      ================================================== ==================================================

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/114106568