In a recent discussion with a coworker a point was made regarding the difference between response.sendRedirect(URL) and RequestDispatcher.forward(). As I understand the essential difference is that the sendRedirect is actually sent directly to the browser. The browser then innitiates a new request from the server for the
JSP. The results in a network hop to have the request issued and elimiates the use of the HTTP Request and Response objects from the JSP since they no longer have addressability to them. Contrasted, with the RequestDispatcher.forward where the transfer of control from a
servlet to a JSP happens on the server and all resources are available to the target JSP. A post form "The Serverside" seems to indicate an interesting behavior that I'm having a hard time accepting since disptatch.forward is so widely used. In his post, Paul Strack says the following:
"1) If you use a RequestDispatcher, the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute(). With a sendRedirect(), it is a new request from the client, and the only way to pass data is through the session or with web parameters (url?name=value).
2) A sendRedirect() also updates the browser history. Suppose you have JSP-1 which has a form that targets Servlet-2, which then redirects to JSP-3. With a redirect, the user's address bar will read "http://[host]/JSP-3". If the user clicks the Reload/Refresh button, only JSP-3 will be re-executed, not Servlet-2.
If you use a RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read "http://[host]/Servlet-2". A reload/refresh will execute both Servlet-2 and JSP-3. This can be important if Servlet-2 performs some system update (such as credit-card processing)."
My issue comes with understanding exactly what Mr. Strack is really saying. It does not seem reasonable that the Servlet would be executed as well as the JSP upon a reload/refresh. Especially, in light of the original HTTP Request and Response no longer being available. Could someone either validate his assertion or provide a clearer explanation?
Here is the url to the post on "The Serverside"
http://www.theserverside.com/discussions/thread.tss?thread_id=26425#124872124872 Thanks so much for the insight!
Fran Varin