tags:

views:

336

answers:

1

SendRedirect or requestdispatch ?Which should be more preferred?Which is more efficient?

+3  A: 

They do two very different things, so you cannot just decide on efficiency.

Sending a redirect will send the browser to a different URL. That URL will be visible to the browser. You may or may not want that. For example, after a POST, you should probably redirect to a GET page to avoid that the result page cannot be reloaded without re-posting. On the other hand, you cannot redirect to "pages" that are only accessible from inside the servlet container.

A dispatch is more "efficient" in that there is no extra round-trip, but it only works withing the same web-application context (or at most within the same servlet container if you so set it up). Also, the URL that the user used to access the page in the first place will be different from what a servlet later on in the chain will be called as, which may be confusing. The dispatch pattern is often used for extra processing before or after the real request (in lieu of a ServletFilter), or for error pages.

You can pass along request attributes using a dispatch, but only query parameters on a redirect. You cannot redirect as a POST (so the amount of data that you can attach to it is limited). All query parameters in a redirect are visible to the user.

Thilo