The difference between redirection and request forwarding

Source: Internet
Author: User

Http://www.cnblogs.com/xrq730/p/4915403.html

The process of one redirect

My code is already written, there is a form on the redirectandfoward.jsp page and the form is redirected to redirectandfowardtarget.jsp, so this time the redirection process is:

1, in the specified way (form see method, the direct URL is initiated is get) send a request to the background, background processing Redirect, return a 302 HTTP status code to the client, and in the response with a location to identify the redirect address

2, the address of location as the destination address, the client to initiate another request

The process of one request forwarding

In the specified way (form see method, direct URL initiation is get) send a request to the background, the background directly to the address to redirect, if successful, return status code 200 to the client:

The difference between redirection and request forwarding

1, from the previous redirect and the process of forwarding, we can draw the first difference point, that is, redirect is two requests, forwarding is two requests, so the forwarding speed is faster than the redirect

2. The second difference is also obvious, the address on the address bar after redirection will change, change to the address of the second request, after forwarding the address bar will not change the address, or the first time the requested address

Online about the difference between redirection and request forwarding There are many, there are many differences, but summed up is nothing more than this two points, and then many of the differences are also the expansion of the two points, so the difference between redirection and request forwarding understand the difference between the two points can be. There is another point, not the difference between redirection and request forwarding, but it's worth noting that the code after redirection and request forwarding runs until the method ends or the return statement is encountered

Interpretation of redirect and request forwarding URL parameters

We know that in the Java Web, a servlet is writing the redirect and forwarding code, respectively:

1, Response.sendredirect ("XXX");

2, Request.getrequestdispatcher ("XXX"). Forward (request, response);

The parameters in this parenthesis are also certain rules, my own summary is as follows:

1, redirect, if the parameter is "Http://xxxxxxxxxx", then redirects to the specified redirect address, whether the redirect address is internal or external

2, if the parameter is not "http://xxxxxxxxxx" appearance, but the general wording, then all are tomcat internal jump ( Note is tomcat internal jump instead of the project internal Jump ), redirection and request forwarding when there are some differences:

(1) Redirect, if XXX starts with "/", then the destination address is Http://ip:port/XXX, and if you do not start with "/", the target value is http://ip:port/project name/xxx

(2) Request forwarding, regardless of whether XXX with "/" Start, all jump to http://ip:port/project name/xxx

Personal suggestions for redirection and request forwarding selection

Page jumps suggest using redirects, and there is an internal servlet jump that recommends using forwarding.

Reasons to select Redirect as a page jump

1, the forwarding request address will not change, which leads to developers do not know the actual forwarding of the target page, for developers it is really a bad thing, do not know that the target page will be wrong to the location of the problem caused a lot of trouble

2, if the developer in the servlet to do time-consuming query database operations, and then put in the session, let the target page to display the session to the user. At this point, if the use of forwarding, then in the refresh of this page, the servlet will be asked again, that is, the query database--set session This step to do again, and in fact, this is meaningless. Redirection is not a problem, because after redirection is a. JSP page, refreshing the page is nothing more than having the page reload.

3, forwarding can only jump in the station, redirect can jump to any desired address----as long as this address exists, so the request page when forwarding compared to redirection also has limitations

Therefore, although the speed of the forwarding of the target is fast, but from the angle of avoidance problem, page jump using redirection is definitely better than forwarding.

Select the reason for forwarding as an internal servlet jump

If the code logic is servleta->servletb->*.jsp, using forwarding, then the three operations are in a single request, and if you use redirection, then the client will initiate three requests, which is really unnecessary. Therefore, if the code logic contains internal servlet jumps, using forwarding will make a good choice.

The difference between redirection and request forwarding

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.