Monday, October 29, 2007

JSP: sendRedirect() vs. getRequestDispatcher()

Today my other team member was facing a problem and the request objects were destroed when he was using request.sendRedirect(). He was struggling with it and came to me. Actually there is other method to use when you want to pass the origional request objects, that is getRequestDispatcher();


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

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.


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

No comments: