response.sendRedirect(URL)

Hi,

I was reading response.sendRedirect(URL) as follows


response.sendRedirect(URL) used for

 sending response back to the browser which in effect say request cannot be handled by me and go to this URL instead. one cannot use this method if one already written something to the response and  URL can be relative to the location of this servlet
 



I have not understood clearly what it means. Why the browser cannot hadle request. Instead it is asking to go to URL. I was thinking URL also eventually run/handled by browser only right. In that case what is the use or purpose of response.sendRedirect(URL).

What practical scenarios we use this. Are there are any alternates to this.


Please advise.

Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
This is completely basic http.

response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); // 301
response.setHeader("Location", "http://somewhere.com/");

will return a header to the browser which will then immediately go to the URL specified - so the browser is handling the redirection based on the instructions you give it from the server.

You will as explained, use this when you detect on the server that the request cannot be served - for example the user asks for an item that no longer exists and you want to redirect to the home page or back to the search form

If you want the browser to not bother the server if the client finds an issue, use javascript and for example location.replace("http://somewhere.com/");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
>>response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); // 301
response.setHeader("Location", "http://somewhere.com/");



is same as

  response.sendRedirect(URL)

Please advise
0
Amitkumar PSr. ConsultantCommented:
0
gudii9Author Commented:
>>>For example, database update and data display can be separated by redirect. Do the PaymentProcess and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PyamenProcess will not be repeated. But if you use forward in this scenario, both PaymentProcess and displayPaymentInfo will be re-executed sequentially, which may result in incosistent data.





I am not clear on above paragraph. can you please elaborate
0
gudii9Author Commented:
Please advise
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.