Friday, June 15, 2012

Passing Control Between Web Pages

Forward vs Redirect

These are the two methods of transferring the control between web pages,the purpose of both is almost same but there is some difference which i am going to tell you here.

Redirect: If you want to implement this redirecting process in jsp you have to follow this syntax:
<c:redirect url="targetPageURL">
>> where c is nothing but prefix for core JSTL actions we set at the beginning of every jsp page to use Java Standard Tag Library .

we include tag libraries in our page this way :
<%@taglib prefix="c" uri=""%>
Note:you can provide some other prefix aswell but c is recommended so that you don't catch a reserved keyword.
Now moving to the main point
Redirecting will pass the control to the page specified in the url attribute of tag. This control transfer make browser to request the new page in a formal way,and the address in the address bar of browser changes to the url of new page as the page is loaded. 

<jsp:forward page="targetPageURL"> 
this way the new page will be loaded for processing the same request by the jsp container implicitly without telling Browser of client machine about control transfer.
So the URL in the address bar of the browser will remain same even though new page has been loaded. 
Another specification of foreward method is,it automatically transfers all the parameters with Request scope to the nest page.
Differences b/w both:

Forward Redirect
-> It request the new page implicitly without letting browser know about the control transfer. -> It request the new page with the help of browser in a formal way.
-> URL in the address bar of the browser remain intact indicating previous page. -> URL changes to the new page automatically
-> Transfers all the request parameters to the target page. -> It doesn't transfer request scope parameters to the target page.

Advatage of Forward: It is fast because browser don't interfere in requesting of target page.
Drawback of Forward: URL doesn't change with the loading of target page,so if the user try to refresh the target page,browser will again open the previous page because the URL in the address bar is still indicating  the previous page.

Advantage of Redirect: If user try to refresh the target page,there's no problem.
Disadvantage of Redirect: it is slow because of Request and Response procedure followed by browser adds extra time in loading of target page.

No comments:

Post a Comment