Explain the difference between redirection and request forwarding

table of Contents

Redirect

Request forwarding (Forward)

the difference


Redirect

Redirect: The client browser sends a request to the Web application server. The Web server uses the sendRedirect() method of HttpServletResponse to send the result (the HTTP status code in the header information in the result is 302, and the response is saved in the Location response header field. The returned address) is returned to the client browser; the client browser parses the header information after receiving the server-side result, and then automatically sends a request to the web application server by GET according to the address specified in the header. At this time, the address bar URL When a change occurs, the server receives a new request and returns the result to the client browser. The client browser interprets the execution return result and displays the execution result to the user, and the process ends.
 

package com.zzu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("target.jsp");
	}
}

Request forwarding (Forward)

Forward : The "request" here is a noun. Request forwarding means that the client browser sends a request to the Web server. After receiving the request, the server performs corresponding processing and then forwards the request to another resource (that is, this A "forwarding" operation is performed on the Web server, and the server processes the resource and sends it back to the client.

package com.zzu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}

}

the difference

1. Redirection: When the client sends a request to the server, the request address changes (), and the server returns a new address to the client to continue processing. At this time, the client automatically (without manual participation) accesses the new server's new address. Address, the server returns the content of the new address to the client. Redirection is used for processing. The client and server have executed two requests and two responses, and the browser address bar displays the new address for the second visit.
     Request forwarding: The client sends a request to the server, and the request address changes. The server calls the internal method to directly forward the request to the new address for processing, and then feeds back the processing result to the client. Using the request forwarding method, the client and server only performed one request and one response, and the browser address bar displays the original address of the first visit.
2. The request forwarding method handles the page jump and the new address of the jump. It must be the resource of the web application system, and cannot access the resources of other web applications, and redirection does not have this restriction.
3. Redirection cannot get the data saved in request.setAttribute on a new page, and request forwarding is okay.
4. The WEB-INF folder in the Web project is the safest directory. The resources in this directory can only be obtained by request forwarding, and cannot be obtained by redirection.
 

Guess you like

Origin blog.csdn.net/m0_46383618/article/details/107526816