(1) The Requestdispatcher.forward method can only forward requests to components in the same Web application, and the Httpservletresponse.sendredirect method is not only redirected to other resources in the current application, You can also redirect resources to other applications on the same site, even resources that are redirected to other sites by using an absolute URL. If the relative URL passed to the Httpservletresponse.sendredirect method begins with "/", it is relative to the root of the entire Web site, and if the relative URL specified when the RequestDispatcher object is created starts with "/". It is relative to the root directory of the current Web application.
(2) After the access process that calls the Httpservletresponse.sendredirect method redirects, the URL displayed in the browser's address bar changes, and the initial URL address becomes the target URL of the redirect, and the call Requestdispatcher.forward The browser address bar keeps the initial URL address intact after the request-forwarding process for the method is completed.
(3) The Httpservletresponse.sendredirect method responds directly to the request of the browser, and the result of the response is to tell the browser to re-issue the request for another URL, a process like a person nicknamed "the browser" who wrote to find Zhang San to borrow money, Zhang San replied that there is no money, let "browser" to find John Doe borrow, and will John Doe now the address of the communication to the "browser." So, "browser" also press Zhang San to provide correspondence address to John Doe letter to borrow money, John Doe received the letter to the money to the "browser". As can be seen, the browser sent out two letters and received two replies, and the browser also knew that the money he borrowed was from John Doe's hand. The Requestdispatcher.forward method forwards the request to another resource on the server side, and the browser only knows that the request has been made and the result has been answered, and does not know that there is a forwarding behavior inside the server program. This process is like nicknamed "Browser" people write to find Zhang San borrow money, Zhang San no money, so Zhang San find John Doe borrowed some money, even can add some of their own money, and then remit the money to the "browser." As can be seen, the "browser" only sent a letter and received a reply, he only knew from Zhang San to borrow money, do not know that a part of the money from John Doe hand.
(4) The caller and the callee of the Requestdispatcher.forward method share the same request object and the response object, which belong to the same access request and response procedure, while the Httpservletresponse.sendredirect method calls And the callee use their respective request objects and response objects, which belong to two separate access request and response procedures. For jumps between internal resources of the same Web application, especially before a jump, some pre-processing of the request And to pass the preprocessing results using the Httpservletrequest.setattribute method, you should use the Requestdispatcher.forward method. Redirection between different Web applications, especially if you are redirecting resources to another Web site, should use the Httpservletresponse.sendredirect method.
(5) Neither the Requestdispatcher.forward method nor the Httpservletresponse.sendredirect method can have the content actually output to the client until they are called. If something is already in the buffer, the content will be purged from the buffer.
Requestdispatcher.forward and Httpservletresponse.sendredirect