Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of girionis
girionis
Flag of Greece image

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.
Avatar of gudii9

ASKER

is

response.setStatus()
response.setHeader()
does same thing as explained in this video??
http://www.youtube.com/watch?v=CeMqHGXQuPc
Please advise
Avatar of gudii9

ASKER

>>>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
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.
Avatar of gudii9

ASKER

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)
Avatar of gudii9

ASKER

Please advise
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial