The difference between Forward and Redirect

The user sends an HTTP request to the server, and the request may be processed by multiple information resources before returning to the user. Each information resource uses the request forwarding mechanism to forward requests to each other, but the user does not feel that the request is forwarded. According to the different forwarding methods, what is the difference between direct request forwarding (Forward) and indirect request forwarding (Redirect)? This article comprehensively explains the principles and differences between the two request forwarding methods while answering the question.

[Frequency] 
[Key test site]

The meaning of request forwarding;
The principle of Forward forwarding request; The principle of
Redirect forwarding request.
[Exam Question Analysis]

Forward and Redirect represent two request forwarding methods: direct forwarding and indirect forwarding.

Direct forwarding method (Forward), the client and the browser only send a request, Servlet, HTML, JSP or other information resources, the second information resource responds to the request, in the request object request, the saved object is for each one. Information resources are shared.

The indirect forwarding method (Redirect) is actually two HTTP requests. When the server responds to the first request, the browser sends a request to another URL to achieve the purpose of forwarding.

To give a common example:

direct forwarding is equivalent to: "A asks B to borrow money, B says no, B asks C to borrow, and if it fails to borrow, it will pass the message to A";

indirect forwarding is equivalent to: "A Ask B to borrow money, B says no, and ask A to ask C to borrow money."

The principle of the two is explained in detail below:

1: Indirect request forwarding (Redirect)

Indirect forwarding, sometimes called redirection, is generally used to avoid abnormal user access. For example, if a user accesses background resources without logging in, the servlet can redirect the HTTP request to the login page, allowing the user to log in and then access it later. In the Servlet, by calling the SendRedirect() method of the response object, the browser is told to redirect the access to the specified URL. The sample code is as follows:

......
//Method for processing get requests in Servlet
public void doGet(HttpServletRequest request, HttpServletResponse response){
//The request is redirected to another resource
    response.sendRedirect("URL of the resource");
}
........

The process of indirectly forwarding the request shown in the figure above is as follows:

the browser sends a request to Servlet1 Access request;
Servlet1 calls the sendRedirect() method to redirect the browser to Servlet2; the
browser sends a request to servlet2;
Servlet2 finally responds.
Two: Direct request forwarding (Forward)

The direct forwarding method is used more. Generally speaking, the request forwarding refers to the direct forwarding method. Web applications will mostly have a controller. It is up to the controller to control which information resource the request should be forwarded to. Then these information resources process the request, and after processing, it may be forwarded to other information resources to return to the user. This process is the classic MVC pattern.

The javax.serlvet.RequestDispatcher interface is an interface that the request forwarder must implement. The Web container provides the Servlet with an object that implements this interface. The purpose of request forwarding is achieved by calling the forward() method of this interface. The sample code is as follows:

 … ..
    //Methods for processing get requests in Servlet
public void doGet(HttpServletRequest request , HttpServletResponse response){
     //Get the request forwarder object, which points to
   RequestDispatcher through the parameter setting of getRequestDisPatcher() requestDispatcher =request.getRequestDispatcher(" The URL of the resource");
    //Call the forward() method to forward the request     
   requestDispatcher.forward(request,response);   
}
...


The process of directly forwarding the request shown in the figure above is as follows:

the browser issues an access to Servlet1 Request;
Servlet1 calls the forward() method to forward the request to Servlet2 on the server side; Servlet2
finally responds.
Skill: In fact, through the browser, you can observe the request forwarding method used by the server. When a single hyperlink is used, the address bar of the browser will display the address of the current request. If the server responds, the address is found. If the address of the column has changed, it proves to be an indirect request forwarding. Conversely, if the address has not changed, it represents a direct request for forwarding or no forwarding.

Q: What is the principle and difference between direct forwarding and indirect forwarding?

A: Forward and Redirect represent two request forwarding methods: direct forwarding and indirect forwarding. Corresponding to the code, they are the forward() method of the RequestDispatcher class and the sendRedirect() method of the HttpServletRequest class.
For the indirect method, when the server responds to the first request, the browser sends a request to another URL to achieve the purpose of forwarding. It is essentially two HTTP requests, corresponding to two request objects.

For the direct method, the client browser only issues a request once, the servlet forwards the request to the servlet, HTML, JSP or other information resources, and the second information resource responds to the request, and the two information resources share the same request object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326432911&siteId=291194637