This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Priety Sharma wrote:
Since this resoucre is outside the web application.
Can the user return to the application from this resource?
I am still confused about the working of sendRedirect()
If it is simple www.yahoo.com, yes you can put a link (how you put that link in Java Servlet without bad practise? ). But, what if you want to redirect to a "resource" like another servlet of another web application? This is where sendRedirect() comes. You can use sendRedirect to send control to another component outside the current web application. The browser will invoke a new request to the resource being redirected after doing authentication etc.
Hope this clears you doubt.
Sai Surya, SCJP 5.0, SCWCD 5.0, IBM 833 834
http://sai-surya-talk.blogspot.com, I believe in Murphy's law.
Sai Surya wrote:If it is simple www.yahoo.com, yes you can put a link
Umm, no. How on earth are you going to get a link onto Yahoo's page?
I am just referring putting a link from Servlet (using out.println("[anchor tag to yahoo]") instead of using sendRedirect ). I know its bad practise. However, please ignore my previous answer. Apologies for wrong interpretation. rel="nofollow">
Just don't get trapped by the word resource, weather it is, inside the application or outside of it, it doesnt affect the redirect. sendRedirect would send browser a status code 302 (temporary redirect) along with another URL where the request should be redirected. The URL can be relative to your application or an absolute URL to some another website, that doesn't make any difference for browser. Upon receiving request, browser will create a fresh request and send it to the url specified in response. It is a fresh request.
Using a sendRedirect one can forward a request to a resource outside the web server.
Why cant we give a simple link like "http://www.yahoo.com" instead of a sendRedirect, as anyways the control is going out of the application.
What is the exact reason for using a sendRedirect?
sendRedirect is used in a code, based on the logic, "dynamically" you will redirect the request to some other page. For example, if you get a request to show the details of the person who actually not logged in, now you redirect the page for login page.
Where as the LINKS are predifined in the page for a particular page, this is not a dynamic one handled in logic.
Is there any way control returns to our application?
When you want to have your control on the application when it goes out of scope, you have to use FRAMES or IFRAMEs. For example, while click on the result image of Google Images, it takes you to actual site of the image. But there will be a google's frame on top the page. By having a frame like this, you can still have a control on the application.
So now what I get is:
Browser makes a fresh request to the specified URL.
sendRedirect is mainly used to go to resources outside your application.
To return back to your application the only way is back button.
One doutb still: Why does one need to use sendRedirect for a resource internal to the application, why cant just a forward but used?
A redirect is the server telling the browser "what you are looking for is over there"
The browser then makes a new request to that "over there" location.
A forward is an internal server operation. The server does it invisibly to the browser.
You ask for "x". The server forwards internally to "y" to handle the request.
As far as the browser is concerned it asked for "x" and got it. It knows nothing about "y"
Why would you use sendRedirect for an internal resource over forward?
The classic use case is the PRG - Post-Redirect-Get Pattern to get around the double submit problem