• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets

 
kiran keshoju
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between sendRedirect() and forward() in servlets
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sendRedirect occurs at the client side. So it is more or less equals to the client typing the new URL in his address of the browser.

In other words browser is asked to request a url

This takes a greater turn around time i.e. the new URL will be invoked from the client.

forward is transparent to the user and he doesn't know that there has been some forward to other url. This is a server side redirection and infact takes lesser turn around time
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that was not mentioned is the intent of each of the calls. I recommend searching the forum for a more complete outline, but it important to realise that these are different pieces of functionality and are not necessarily interchangeable.

Take as an example two common pieces of functionality.

The first is to display an error page when some processing fails. In this case, you can put any additional information on the request, forward to the display page, and everything works fine. The client's browser still has the same URL so if they reload the page it will repeat the previous step.

The second is to submit some data then send back to a menu. If you use forward, the URL doesn't change, so if the user refreshes the screen it resubmits the data! In this case, it is better to process the data then sendRedirect to the menu page. This forces the client to send a second HTTP request, asking for the menu page.

In the first example, if you use sendRedirect rather than forward you can't place information on the request context, and information gets lost once the new request comes in. You could place the information on the session and still use sendRedirect, but placing request-scope data on the session is a recipe for disaster. This is another story.

Hope this helps,
Dave.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Bartender, I have never thought of it. its really worth reading.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic