-->

Redirect or forward

2020-07-16 12:15发布

问题:

Looking through some legacy code I have in front of me using struts one, I see:

<global-forwards>
     ...
     <forward name="accessDenied" path="/www/jsp/AccessDeniedForm.do" redirect="true" />
</global-forwards>

So it's just a global forward to send to a access denied page. I am curious about the decision to redirect as opposed to forward. What are the advantages and disadvantages of using it?

回答1:

What are the pro's and con's of using it?

Before discussing pro's and con's of using that forward element with redirect set to true, let's understand what is actually going on with that configuration. When redirect is set to true in the forward element, a redirect instruction should be issued to the user-agent so that a new request is issued for this forward's resource. This link will probably provide detail information that you need.

The default value for redirect is to false, essentially when the forward element is called, it forward to that path specified and that's it. If you are setting redirect to true, take for example, the browser will make another request. So I think with these said, you probably know or have an idea the pro and con if you really want to use it.



回答2:

In redirect, the control can be directed to different servers or even another domain name.The redirect takes a round trip.When a redirect is issued , it is sent back to the client , and redirected URL information is in the header instructing the browser to move to the next URL. This will act as a new request and all the request and response data is lost.

In forward , the forwarding is done from server side , the client browser URL do not change.the data is also not lost.It is just like a browser page refresh. Whatever data posted in the first submit is resubmitted again.So use it with caution.

Both forward and redirect are used in different scenarios ,the global forward should be redirect because it is an error situation.

Redirect is slower as it needs a roundtrip.Forwards are faster.



回答3:

If you specify

redirect="true", Struts uses a client-side redirect

[response.sendRedirect()]

. The JSP will be invoked by a new browser request, and any data stored in the old request will be lost.