Sunday, 24 April 2016

forward vs. sendRedirect

 
  • A forward is performed internally by the servlet.
  • A redirect is a two step process, where the web application instructs the browser to fetch a second URL.

    In Forward, the browser is completely unaware that it has taken place, so its original URL remains intact.

    In Redirect, the browser, in this case, is doing the work and knows that it's making a new request so it changes the URL.

  • When forward is used we can pass data to the forwarded jsp / servlet using request.setAttribute() as the same request and response objects available the forwarded servlet/jsp.
  • In Redirect, there is only one way to pass data is through session or using request parameters (url?name=value). 

  • With RequestDispatcher, you can redirect only to a page within current servlet context.
  • sendRedirect() forwards a requests to a resource outside of the current web application

  • With forward() you can use only relative URLs
  • sendRedirect() works with absolute URLs.

  • Forward is marginally faster than redirect (Redirect requires two browser requests)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.