Difference between sendRedirect() and forward() in jsp servlet Java? 2013

View previous topic View next topic Go down

Difference between sendRedirect() and forward() in jsp servlet Java? 2013

Post by Admin on Sun Sep 15, 2013 4:03 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">sendRedirect() and forward() methods are applicable to both servlets and jsp in java. Since jsp converts to a servlet itself, these methods can be used inside a scriplet in a jsp.<br><br><b>sendRedirect()</b>:<br>This method is called from HttpServletResponse object. This method from response object expects an URL where the response is required to redirect. This sends the user to different location located on a different server altogether. This happens on a client side or browser. The new address will be visible in the browser.<br><br>Redirect sets the response status to 302, and the new url in a <b>Location </b>header, and sends the response to the browser. Then the browser, according to the http specification, makes another request to the new url.</br></br>The most general example of redirect is the payment gateway. Most of the application tie up with the third party merchant payment gateways. So when an order placed by the customer is done and the following step is to do the payment, the application redirects the browser to altogether new location payment gateway.<br><br>Redirect goes to the browser in form of a 301 response and then gets back redirected using Location header.</br></br> <b>forward()</b>:<br>This method is called from RequestDispatcher Interface.<br>forward(ServletRequest request, ServletResponse response)<br><br>This method sends the client request on to different resource within the same web application for further processing. The new resource could be any servlet or jsp within the application. This happens on a server side.<br><br> Below are the differences in terms of interview language:<br><br> <table border = "1"><tr><td>sendRedirect() </td><td>forward() </td></tr> <tr><td>In this case, request gets transferred to resource outside the application </td><td>In this case, request gets transferred to resource within the application </td></tr> <tr><td>This happens on the client side </td><td>This happens on the server side </td></tr> <tr><td>New URL of some other web application will appear in the browser </td><td>URL of the same web application remains on the browser </td></tr> <tr><td>In this case, existing request and response objects are lost </td><td>In this case, existing request and response objects are preserved </td></tr> <tr><td>Classic example, some sort of advertisement on the web page </td><td>Call another servlet o jsp within the application </td></tr></table> <br /><table border="0" id="amazon_body_ads_com" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl-20&o=1&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B00DVFLJDS" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=B0075SUHWG&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=B006P88VSE&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=0316206849&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table> <table border="0" id="amazon_body_ads_in" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=8173666024&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=0006479901&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=1847370292&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=9350042096&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table> <table border="0" id="amazon_body_ads_uk" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B000C1ZDTU" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007VBGTX8" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B009SJCW3C" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B009VK73FA" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td></tr></tbody></table> <table border="0" id="amazon_body_ads_ca" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007HCCOD0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007FXPWCQ" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007XZM6VG" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B00834SJS0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table></br> <script>//<![CDATA[ var tokens = document.domain.split('.'); var domain = tokens[tokens.length - 2]; if(tokens[tokens.length - 1]=="uk") { document.getElementById("amazon_body_ads_uk").style.display=""; } else if(tokens[tokens.length - 1]=="com") { document.getElementById("amazon_body_ads_com").style.display=""; } else if(tokens[tokens.length - 1]=="in") { document.getElementById("amazon_body_ads_in").style.display=""; } else if(tokens[tokens.length - 1]=="ca") { document.getElementById("amazon_body_ads_ca").style.display=""; } else{ document.getElementById("amazon_body_ads_com").style.display=""; } //]]></script> </div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">sendRedirect() and forward() methods are applicable to both servlets and jsp in java. Since jsp converts to a servlet itself, these methods can be used inside a scriplet in a jsp.<br><br><b>sendRedirect()</b>:<br>This method is called from HttpServletResponse object. This method from response object expects an URL where the response is required to redirect. This sends the user to different location located on a different server altogether. This happens on a client side or browser. The new address will be visible in the browser.<br><br>Redirect sets the response status to 302, and the new url in a <b>Location </b>header, and sends the response to the browser. Then the browser, according to the http specification, makes another request to the new url.</br></br>The most general example of redirect is the payment gateway. Most of the application tie up with the third party merchant payment gateways. So when an order placed by the customer is done and the following step is to do the payment, the application redirects the browser to altogether new location payment gateway.<br><br>Redirect goes to the browser in form of a 301 response and then gets back redirected using Location header.</br></br> <b>forward()</b>:<br>This method is called from RequestDispatcher Interface.<br>forward(ServletRequest request, ServletResponse response)<br><br>This method sends the client request on to different resource within the same web application for further processing. The new resource could be any servlet or jsp within the application. This happens on a server side.<br><br> Below are the differences in terms of interview language:<br><br> <table border = "1"><tr><td>sendRedirect() </td><td>forward() </td></tr> <tr><td>In this case, request gets transferred to resource outside the application </td><td>In this case, request gets transferred to resource within the application </td></tr> <tr><td>This happens on the client side </td><td>This happens on the server side </td></tr> <tr><td>New URL of some other web application will appear in the browser </td><td>URL of the same web application remains on the browser </td></tr> <tr><td>In this case, existing request and response objects are lost </td><td>In this case, existing request and response objects are preserved </td></tr> <tr><td>Classic example, some sort of advertisement on the web page </td><td>Call another servlet o jsp within the application </td></tr></table> <br /><table border="0" id="amazon_body_ads_com" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl-20&o=1&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B00DVFLJDS" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=B0075SUHWG&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=B006P88VSE&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=0316206849&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table> <table border="0" id="amazon_body_ads_in" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=8173666024&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=0006479901&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=1847370292&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=9350042096&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table> <table border="0" id="amazon_body_ads_uk" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B000C1ZDTU" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007VBGTX8" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B009SJCW3C" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B009VK73FA" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td></tr></tbody></table> <table border="0" id="amazon_body_ads_ca" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007HCCOD0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007FXPWCQ" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007XZM6VG" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B00834SJS0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table></br> <script>//<![CDATA[ var tokens = document.domain.split('.'); var domain = tokens[tokens.length - 2]; if(tokens[tokens.length - 1]=="uk") { document.getElementById("amazon_body_ads_uk").style.display=""; } else if(tokens[tokens.length - 1]=="com") { document.getElementById("amazon_body_ads_com").style.display=""; } else if(tokens[tokens.length - 1]=="in") { document.getElementById("amazon_body_ads_in").style.display=""; } else if(tokens[tokens.length - 1]=="ca") { document.getElementById("amazon_body_ads_ca").style.display=""; } else{ document.getElementById("amazon_body_ads_com").style.display=""; } //]]></script> </div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum