JSP redirect

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

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?

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.

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.

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.

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

Posts: 12

posted 6 years ago

[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?