• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlets

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between sendRedirect() and forward() in servlets
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Bartender, I have never thought of it. its really worth reading.
reply
    Bookmark Topic Watch Topic
  • New Topic