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.
--: Biju Nair :--
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