Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sendRedirect or request Dispatch is more efficient?

Tags:

java

jsp

servlets

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

like image 913
Warrior Avatar asked Aug 11 '26 04:08

Warrior


1 Answers

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.

like image 165
Thilo Avatar answered Aug 12 '26 17:08

Thilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!