jsp - RequestDispatcher.forward() vs HttpServletResponse.sendRedirect() -
what conceptual difference between forward()
, sendredirect()
?
requestdispatcher - forward() method
when use forward method, request transfer other resource within same server further processing.
in case of forward, web container handle process internally , client or browser not involved.
when forward called on requestdispatcher object pass request , response objects our old request object present on new resource going process our request.
visually not able see forwarded address, transparent.
using forward () method faster send redirect.
when redirect using forward , want use same data in new resource can use request.setattribute () have request object available.
sendredirect
in case of sendredirect, request transfer resource different domain or different server further processing.
when use sendredirect, container transfers request client or browser url given inside sendredirect method visible new request client.
in case of sendredirect call, old request , response objects lost because it’s treated new request browser.
in address bar, able see new redirected address. it’s not transparent.
sendredirect slower because 1 round trip required, because new request created , old request object lost. 2 browser request required.
but in sendredirect, if want use have store data in session or pass along url.
which 1 good?
its depends upon scenario method more useful.
if want control transfer new server or context , treated new task go send redirect. generally, forward should used if operation can safely repeated upon browser reload of web page not affect result.
Comments
Post a Comment