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

response.redirect and jsp:forward

 
Ranch Hand
Posts: 231
  • 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 response.sendRedirect and jsp:forward? Which one is better?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you'd like to be sent to another page within your web application use jsp:forward. Use send redirect if you want to direct the user to an external site.
Hope this helps,
JEB
 
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
I also dropped another explaination here. THis is a very common question so you may want to try the search functionality.
Dave.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both <jsp:forward> and response.sendRedirect is used to send the user to a JSP page or servlet or web page that's different from what the user had initially requested. In simple words, if u want to do some processing of user given parametres before u want to direct user to a page, u can use them. For e.g: while validating user through login page, u can check the validity of user and if a valid user u can redirect him to one page and if not valid u can redirect him to an error page or something.
Now when to use <jsp:forward> and when to use response.sendRedirect() ?
To a programmer, the major difference is :
1. The <jsp:forward> tag forwards the HttpServletRequest object with all the HTTP parameters to the destination page. With the response.sendRedirect() method, all HTTP parameter information is lost. ie say you had passed the username and password from the login.jsp page to login_action.jsp. In login_action.jsp, u validate the user redirect to customizedpage.jsp. If you use <jsp:forward> to redirect to customizedpage.jsp, then u will be able to get the value of username and password using request.getParameter("") in that page. If u are using response.sendRedirect, u will not get the value.
2. The response.sendRedirect() method sends the redirect HTTP code 301 back to the browser with the new URL. This forces the browser to send another request to the server. Therefore, response.sendRedirect() is much less efficient. ie A separate request is made to the server in the case of response.sendRedirect.
So in short use response.sendRedirect if the original jsp page is unnecessary and u don't want to retain the HTTP parametres. Use <jsp:forward> if u want to.
Also make sure u use them before any output statements or blocks. Else depending on how your JSP container buffers output, you may get an exception that the request can't be forwarded or redirected.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
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