• 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

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic