Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendRedirect method of HttpServiceResponse

 
bob walker
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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)
response.sendRedirect(myURL);
// code continues below.
Now, this works but my question is: Would the method call
not return and continue executing the remainder of the service
method?
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to explicitly return from the method after the redirect. BTW, why are you overriding the service method??
 
Andrew Shafer
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic