response.sendRedirect, RequestDispatcher.forward

Hi,

Session Data Sharing using response.sendRedirect, RequestDispatcher.forward

prefer response.sendRedirect instead of RequestDispatcher.forward

since

User sees JSP URL (user only able to see servlet URL in case RequestDispatcher.forward)

There is need of Two round trips to client (whereas only one with forward)

I wonder why two round trips in case of  response.sendRedirect???? whereas only one for RequestDispatcher.forward

 sendRedirect has advantaes of User can separately visit JSP page, bookmark JSP page

sendRedirect has Disadvantage   user can visit directly JSP page without going through servlet first so JSP data may not be there, so need to code JSP page to detect this situation

How to  code JSP page to detect this situation???

I wonder what these advantages, disadvantages means in detail, practically???

response.sendRedirect, RequestDispatcher.forward concept is there in servlets as well apart from JSP.

I remember reading only RequestDispatcher.forward in servlets not response.sendRedirect


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.

girionisCommented:
I wonder why two round trips in case of  response.sendRedirect???? whereas only one for RequestDispatcher.forward
Because a completely new request is created. The old request object is lost.

How to  code JSP page to detect this situation???
I am not sure I understand this. You are the one who's doing the redirect/forward, why would you want to detect your own actions?
I wonder what these advantages, disadvantages means in detail, practically???
When to use redirect and forward.
0
gudii9Author Commented:
is

response.setStatus()
response.setHeader()
does same thing as explained in this video??
http://www.youtube.com/watch?v=CeMqHGXQuPc
Please advise
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

gudii9Author Commented:
>>>In general, a forward should be used if the operation can be safely repeated upon a browser reload of the resulting web page; otherwise, redirect must be used. Typically, if the operation performs an edit on the datastore, then a redirect, not a forward, is required. This is simply to avoid the possibility of inadvertently duplicating an edit to the database.
More explicitly (in terms of common SQL operations) :

for SELECT operations, use a forward
for INSERT, UPDATE, or DELETE operations, use a redirect



I have not understood above explanation. can you please elaborate
0
girionisCommented:
The author makes a comparison between SQL statement and HTTP actions. He probably means that with SELECT you cannot change the data, so a forward can be used. Otherwise use a redirect.
0
gudii9Author Commented:
There is similar discussion here

http://javapapers.com/jsp/difference-between-forward-and-sendredirect/

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.

where author explained the concept in opposite way..like use forward if you want to do paymentProcessing also along with displayingInfo(where as redirect only for displayingInfo so that if user refreshes paymentProcessing  wil not happen)
0
gudii9Author Commented:
Please advise
0
girionisCommented:
The author means that since a redirect is a brand new request you only see the displayPaymentInfo (the second request), so your payment is not reprocessed again.
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
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 EE

From novice to tech pro — start learning today.