• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP redirect

 
Brian Hsu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need not ""return". When you use sendRedirect, the control is moved to the new page. It is a client side redirect.
 
Brian Hsu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Hsu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic