Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

jsp:foward vs response.sendRedirect

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
What is the difference between jsp:foward and HttpServletResponse.sendRedirect??
Here is some API regarding to each technique.
<jsp:forward />:
This method is used to re-direct, or "forward" the current ServletRequest and ServletResponse to another active component in the application.

HttpServletResponse.sendRecirect:
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs.
But does that really make difference when coding the application?
Thank you.
- Jo
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the main diffrence is that the forward one does the redirecting at the server side while the sendRedirect one redirects to a new URL at the client's side ( the browser).
the sendRedirect one also loses the request and response data you may have put in (like forms attributes etc) while the forward one doesnt.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jo,
One big advantage of sendRedirect() is that you can send your request to another Context.
regards
sanj
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
the main difference between jsp forward and redirect is that the code execution stops after u use the jsp forward while in redirect the code is executed till the end.
For ex. try the foll code in a jsp file with both redirect and forward:
Ex:Checking.jsp

here ..only "i=5" shall be printed out to the console.it means that the code execution has stopped after forward.
but in redirect the code execution does not stop..so it will print both the statements.
so according to me its better to use forward than redirect.
Hope this helps.
bye
geeta
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
jsp:forward is used to trasfer control to some other resource a static or a dynamic page. It is totally on server even the url bar of the client is not change. and also by passing the reference of request and response objects u can use them in the forwarding page.
In case of sendRedirect(), first a error code is send to the client and the connection of http is break. Then a new request is send to the server. This causes a new request and response objects. U can not send attributes with request.
Another difference is that in sendRedirect() the connection is made more than once b/w server and client the process is slow as compered to forward which is totally on server.
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic