File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes sendRedirect method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect method" Watch "sendRedirect method" New topic
Author

sendRedirect method

Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

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?
Is there any way control returns to our application?


Priety.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

sendRedirect() is used in Java code to cause a transition to a new resource (page) under code control. A link on a page allows the user to cause the transition via a click.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

sendRedirect() is used in Java code to cause a transition to a new resource (page) under code control.


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()
Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 460

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()


Hi Priety,

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Priety Sharma wrote:I am still confused about the working of sendRedirect()

What sendRedirect() does is to send a response to the browser with a status code and a URL that tells the browser to make the request to the specified URL.

Priety Sharma wrote:Can the user return to the application from this resource?

With the Back button.

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?
Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 460

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">
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

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.

See
http://en.wikipedia.org/wiki/URL_redirection
HTTPServletResponse.sendRedirect


[Servlet tutorial] [Servlet 3.0 Cook Book]
Vinod Sekhar
Greenhorn

Joined: Feb 03, 2004
Posts: 5
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.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi All,

Thanks for the replies. They are a lot of help.

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?
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027

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
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Stefan,

Thanks for the link on "double submit problem". That is very helpful link.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect method