The difference between redirection and request forwarding in HTTP

1. Calling method

We know that the statements to call forwarding and redirection in servlet are as follows:

request.getRequestDispatcher("new.jsp").forward(request, response);   //转发到new.jsp

response.sendRedirect("new.jsp"); // Redirect to new.jsp

In the jsp page you will also see that forwarding is achieved in the following ways:

<jsp:forward page="apage.jsp" />

Of course , redirection can also be implemented in jsp pages:

<%response.sendRedirect("new.jsp"); %> // Redirect to new.jsp

Second, the essential difference

explain one

In a word, forwarding is server behavior, and redirection is client behavior. Why do I say this, it depends on the workflow of the two actions:

Forwarding process: the client browser sends an http request -- "the web server accepts the request -- "calls an internal method to complete the request processing and forwarding action inside the container -- "sends the target resource to the client; here, the forwarding path It must be the url under the same web container , it cannot be redirected to other web paths, and the request in its own container is passed in the middle . The path displayed in the path bar of the client's browser is still the path of the first visit, that is to say, the client does not feel that the server has forwarded it. The forwarding behavior is that the browser only makes one access request.

Redirection process: the client browser sends an http request - "the web server sends a 302 status code response and the corresponding new location to the client browser -" the client browser finds that it is a 302 response, and automatically sends a new http request Request, the request url is the new location address - "The server finds the resource according to this request and sends it to the client. Here location can be redirected to any URL . Since the browser reissues the request, there is no concept of request delivery. The path bar of the client's browser displays the redirected path, and the client can observe the change of the address. The redirect behavior is that the browser makes at least two access requests.

Explanation two

Redirection is actually two requests

The first time, the client requests A, the server responds, and the response comes back, telling the browser that you should go to B. At this time, IE can see that the address has changed, and the history back button is also lit. Redirects can access resources outside of your own web application. During the redirection process, the transmitted information will be lost.

example:

response.sendRedirect("loginsuccess.jsp");

Request forwarding is the transfer of the processing power of a request/response internally by the server to another

For the client, it only knows the A that it first requested , but does not know the middle B , or even C and D. The transmitted information is not lost.

example:

       RequestDispatcher dis=request.getRequestDispatcher(“loginsuccess.jsp”);

       Dis.forward(request,response);

Explanation three

Suppose you go to apply for a license

Redirection: You go to Bureau A first. The person in Bureau A said, "This matter is out of our control, go to Bureau B. " Then, you exit from Bureau A and go to Bureau B by yourself .

Repost: You went to Bureau A first. After Bureau A watched it, you knew that Bureau B should take care of this matter , but instead of sending you back, he asked you to sit for a while, and then he went to the back office and contacted the person in B. After letting them do it, send it over.

 

Source: http://www.cnblogs.com/yqin/archive/2010/06/07/1810454.html

Guess you like

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