Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the response code from a redirect

 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some code, which goes something like this



The URL where we do the sendRedirect is an API which handles authentication. I would like to know if I can programmatically get the response code of that redirection, e.g. if it returned an error such as authentication failed, I could insert some code after the redirect to handle that error.

Is this possible? I tried googling for some code examples and looked at the Servlet API's but haven't really found anything useful.
[ January 14, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aaron John:
... I could insert some code after the redirect to handle that error. ..
Is this possible?


No, You have a fundamental misunderstand of how a redirect works.

The sendRedirect method does not cause the redirect to happen immediately. Rather, it merely sets up the response headers of the current request as appropriate in order to cause the browser to perform the redirect.

So any code that you place after the sendRedirect will execute long before the response is sent to the browser to cause the new request to be initiated.
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


No, You have a fundamental misunderstand of how a redirect works.

The sendRedirect method does not cause the redirect to happen immediately. Rather, it merely sets up the response headers of the current request as appropriate in order to cause the browser to perform the redirect.

So any code that you place after the sendRedirect will execute long before the response is sent to the browser to cause the new request to be initiated.


So to clarify what you said, if I do a call to sendRedirect(), it just sets a header in the response back to the browser. The browser receives the response, and automatically creates a new request to the URL that I specified as an argument to sendRedirect?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. An important point is that the call to sendRedirect doesn't really do anything except set headers that will be sent to the browser whenever the response gets sent back.

It might better have been named setupHeaderForRedirect().
[ January 14, 2007: Message edited by: Bear Bibeault ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic