What is the difference between forwarding and redirection?

Source: Internet
Author: User

Difference 1:
URL changes in the browser during redirection
The URL on the browser remains unchanged for forwarding.
Difference 2:
Redirection actually generates two requests.
Only one request is forwarded.
Redirection:
Send a request --> run the server --> respond to the request, and return a new address and response code to the browser --> the browser determines that the response is redirected based on the response code, A new request is automatically sent to the server. The request address is the previous address --> Server Run --> respond to the request to the browser.
Forwarding:
Send a request --> run the server --> Reset the request, for example, request. setAttribute (name, value) --> get the webpage of the address based on the forwarded address --> respond to the request to the browser
Difference 3:
The URL for redirection can be any URL
The forwarded website must be the website of this site.
Details:
Redirection: all variables stored in previous requests are invalid and enter a new request scope.
Forward: the variables stored in the previous request will not expire, Just Like splicing the two pages together.
Body start:
First, they look different. Their calls are as follows:
Request. getRequestDispatcher ("apage. jsp"). forward (request, response); // forward to apage. jsp
Response. sendRedirect ("apage. jsp"); // redirect to apage. jsp
On the jsp page, you will also see the following method for forwarding:
<Jsp: forward page = "apage. jsp"/>
When I was a beginner at jsp, I was very confused about these two concepts. When I looked at other people's examples, I was confused and did not know when to use them. I hope the following explanation will be helpful to you.
When it comes to forwarding and redirection, you have to mention the request scope. Many beginners know that a new request is created when we submit a form. In fact, when we click a link, a new request is also created. How big is a request acting on? For example:
There is a link in page a. jsp <a href = "B. jsp? Id = 1 "> This is a link to B, and it also includes a parameter </a>. When we click this connection, a request is generated. For clarity, we call it requestA-> B. Now, we can get information from this request on the B. jsp page. In B. jsp, you can write out. println (request. getParameter ("id") for testing. The following is more complex: Add the following statement to the B. jsp page:
Request. setAttribute ("name", "funcreal ");
Out. println (request. getAttriblute ("name"); // The value of the name variable is displayed successfully.
Add another link in B. jsp: <a href = "c. jsp? Age = 23 "> This is a link to c with a parameter </a>. When we click this connection, a new request is generated, at this time, requestA-B is in peace, and the new request is called requestB-C. Similarly, in c. jsp, we can only access the age variable, because the id and name variables both belong to requestA-B, and no longer exist. The source code is as follows:
A. jsp
<% @ Page contentType = "text/html; charset = GBK" %>
<Html>
<Body bgcolor = "# ffffff">
<A href = "B. jsp? Id = 1 "> point to B. jsp, and a parameter id = 1 is included. RequestA-B is now born </a>
</Body>
</Html>
B. jsp
<% @ Page contentType = "text/html; charset = GBK" %>
<Html>
<Body bgcolor = "# ffffff">
<%
Out. println ("id =" + request. getParameter ("id "));
Request. setAttribute ("name", "Func Real ");
Out. println ("name =" + request. getAttribute ("name "));
%>
<A href = "c. jsp? Age = 23 "> requestA-B is over. Point to c. jsp, with a parameter age = 23 </a>
</Body>
</Html>
C. jsp
<% @ Page contentType = "text/html; charset = GBK" %>
<Html>
<Body bgcolor = "# ffffff">
<%
Out. println ("id =" + request. getParameter ("id "));
Out. println ("name =" + request. getAttribute ("name "));
Out. println ("age =" + request. getParameter ("age "));
%>
</Body>
</Html>
So what is forwarding? Add a page named d. jsp, and add a sentence <jsp: forward page = "d. jsp"/> in front of </body> in c. jsp.
D. jsp
<% @ Page contentType = "text/html; charset = GBK" %>
<Html>
<Body bgcolor = "# ffffff">
The claw of requestB-C has reached the d. jsp page.
<%
Out. println ("age =" + request. getParameter ("age "));
%>
</Body>
</Html>
Run the program and you will find that the content on Page c is not displayed, because forward is automatically executed, even though it is c. jsp, but in fact, the browser shows d. jsp content, and see from B. jsp parameters. You can simply understand this: Forwarding is to extend the scope of requestB-C, <jsp: forward page = "d. jsp "/>, this sentence is actually c. jsp and d. jsp is stuck together, and they are like in a page.
If you have used struts, you will know why in the Action, the last sentence is almost always mapping. findForward ("xxx. Because all the request scope variables we set in this Action will be used on the next page (maybe another Action), we need to forward them.
Summary:
Using redirection and forwarding is not a habit. But under what circumstances it must be used.

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.