Difference between forward and redirect in Java

Source: Internet
Author: User
Difference between forward and redirect
1. the URL in the browser address bar remains unchanged after the forward redirection is used.
The address bar of redirect will be changed to the new address after the jump.
2. Forward redirects can only be other Web components of the same web application.
Redirect can be any URL.
 
3. when a client (browser) requests a JSP that uses forward for redirection, the client (browser) only interacts with the server once. The redirection of forward is implemented by the server, and the customer does not know that there is a redirection; when a client (browser) requests a JSP that uses redirect for redirection, redirect
The browser will first send a response to the browser, then the browser receives the response and then sends a requeset to the server, and then the server sends a new response to the browser. The client (browser) interacts with the server twice.
4. when you use redirect to jump to a page, due to (3) reasons, when the client requests the server, the parameters following the URL are no longer displayed during the jump. Therefore, these parameters cannot be obtained on the final JSP page, but you can use response. setattribute (
"Name", name) to the next page.

JSP
Example of Using forward and redirect
<%
String forward = "JSP/forward. jsp ";
String redirect = "JSP/redirect. jsp ";
Response. sendredirect (redirect); // use
Redirect through redirect
// Request. getrequestdispatcher (forward). Forward (request,
Response); // use forward to redirect
%>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.