wood burning stoves
The moose likes Servlets and the fly likes sendRedirect method of HttpServiceResponse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect method of HttpServiceResponse" Watch "sendRedirect method of HttpServiceResponse" New topic

sendRedirect method of HttpServiceResponse

bob walker

Joined: Feb 20, 2001
Posts: 14

Maybe someone can shed some light on this subject.
I'm in the service method of my servlet.
The sample code looks like this:
if (request.getSession(false) == null)
// code continues below.
Now, this works but my question is: Would the method call
not return and continue executing the remainder of the service
I see the reDirect happening but I'm curious if its servlet
engine implementation specific whether it returns or not.
I can't seem to find an answer in the O'reilly servlet book.
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
You need to explicitly return from the method after the redirect. BTW, why are you overriding the service method??
Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

I'm not sure about the specification.
Running on Tomcat it appears that the code after the redirect DOES run.
I determined this by sending a redirect and then connecting and executing an update in a database.
The redirected page comes up and the update was made.
This makes the most sense for flexibility of programming, at least to me.

Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
Yes - Java code is java code - the virtual machine doesn't know anything about the fact you are running a servlet. The rest of the method will be executed unless you return. However, the response will have been committed, so you cannot change anything from the standpoint of the client. The browser has already received the redirect.
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
I agree. Here's the link: http://aspose.com/file-tools
subject: sendRedirect method of HttpServiceResponse
It's not a secret anymore!