aspose file tools*
The moose likes JSP and the fly likes JSP redirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP redirect" Watch "JSP redirect" New topic
Author

JSP redirect

Brian Hsu
Greenhorn

Joined: Aug 04, 2011
Posts: 12
Using response.sendRedirect in JSP to go to another page, why do I have to "return" to exit from the current page? Redirect seems pretty obvious to me already: Go to another page (quit the current one).
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

You need not ""return". When you use sendRedirect, the control is moved to the new page. It is a client side redirect.


Ashwin Sridhar
SCJP | SCWCD | OCA
Brian Hsu
Greenhorn

Joined: Aug 04, 2011
Posts: 12
Ashwin Sridhar wrote:You need not ""return". When you use sendRedirect, the control is moved to the new page. It is a client side redirect.


I didn't think so either. I've been debugging my application recently and surprising found that (or thought that) I need to "return". For example I had this following piece of code:

if (user != null && !user.isAdmin())
{
Navigation.redirect(request, response, "page_to_be_redirected_to", databaseConnection);
}

Navigation is my wrapper class that uses sendRedirect method and closes the current database connection. I have some code after this if statement that uses the database connection and it's been giving me a database connection not found error. My logic here is to close the database connection upon a page redirect so I don't understand why I've been getting that error. If I specify "return" after the redirect method then everything works but why?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Of course you need to return. All the redirect call does is to set up headers that tell the browser what to do when the response is sent. There's no magic that makes Java behave differently than it usually does. If you do not return, the rest of the code is executed as would be expected.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I was assuming that once a sendRedirect has been done, control automatically goes to the new page. I have few questions now

1) if the response is not committed, why does it try IllegalstateException when we try try to do somethin with response after a sendRedirect.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

if the response is not committed, why does it try IllegalstateException when we try try to do somethin with response after a sendRedirect.

It should be considered as committed. Check the API


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

sendRedirect will cause a new page to be requested by the browser once the response is sent. It makes no sense to try to do anything, especially emit output, after a redirect has been issued.
Brian Hsu
Greenhorn

Joined: Aug 04, 2011
Posts: 12
Bear Bibeault wrote:Of course you need to return. All the redirect call does is to set up headers that tell the browser what to do when the response is sent. There's no magic that makes Java behave differently than it usually does. If you do not return, the rest of the code is executed as would be expected.


But it's different in a servlet? Once you redirect from a servlet, the rest of the code is not executed even if you don't return.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

But it's different in a servlet?Once you redirect from a servlet, the rest of the code is not executed even if you don't return.

No. It's the normal method execution in Java as Bear explained. Code inside the doGet()/doPost() etc... after your redirect code is executed as normal. That's why you should have a return statement to make sure rest is not executed as it is of no use running the rest of the code once you redirect the response to another URL.
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

jsp:redirect is equivalent to calling sendRedirect(...) from an HttpServletResponse. In both cases, what's really happening at the HTTP level is the application server is sending a special "Location:" HTTP header, which tells the browser that it should move to a different page. The browser follows that location header; it is NOT done server-side. More info - http://en.wikipedia.org/wiki/HTTP_location

Using jsp:forward, jsp:include or a RequestDispatcher in a Servlet happens ENTIRELY on the server, and unlike the redirect, no response is sent to the browser until all forwards/includes are processed. It is NOT part of the HTTP protocol, but part of the Servlet API. Completely different behavior than the HTTP redirect, and very important to keep strait.

OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Brian Hsu
Greenhorn

Joined: Aug 04, 2011
Posts: 12
[quote=Pete Nelson]jsp:redirect is equivalent to calling sendRedirect(...) from an HttpServletResponse. In both cases, what's really happening at the HTTP level is the application server is sending a special "Location:" HTTP header, which tells the browser that it should move to a different page. The browser follows that location header; it is NOT done server-side. More info - http://en.wikipedia.org/wiki/HTTP_location

Using jsp:forward, jsp:include or a RequestDispatcher in a Servlet happens ENTIRELY on the server, and unlike the redirect, no response is sent to the browser until all forwards/includes are processed. It is NOT part of the HTTP protocol, but part of the Servlet API. Completely different behavior than the HTTP redirect, and very important to keep strait.
[/quote]

So in both cases (JSP and Servlet), I do have to explicitly state "return" after redirecting to another page to make sure the rest of the code is not executed?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Yes, this has already been discussed. All the redirect call does is to add headers to the response. Then it returns just like any other Java method. Why would it act differently?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP redirect