This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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
Posts: 65653
129
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
Posts: 65653
129
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 ]
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!