Saturday, January 23, 2010

What are the different cases for using sendRedirect() vs. getRequestDispatcher()?

When you want to preserve the current request/response objects and transfer them to another resource WITHIN the context, you must use getRequestDispatcher.

If you want to dispatch to resources OUTSIDE the context, then you must use sendRedirect. In this case you won't be sending the original request/response objects, but you will be sending a header asking to the browser to issue a request to the new URL.

If you don't need to preserve the request/response objects, you can use either.