This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Query on response.sendRedirect() ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Query on response.sendRedirect() ???" Watch "Query on response.sendRedirect() ???" New topic

Query on response.sendRedirect() ???

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342

Why it is so the any line of code which is there after a response.sendRedirect() method gets executed ?

As we know that incase of sendRedirect() controls is send back to browser. I have written a servlet in which after sendRedirect() method there is one System.out.println statement but after redirect to my jsp page that println statement executed.

Now my confusion is if control goes to my jsp page then how that println statement can execute after sendRedirect() ?

Thanks & Regards
[ June 07, 2004: Message edited by: Bikash Paul ]
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
That is because the response has not been committed. After using this method, the response should be considered to be committed and should not be written to. Just type
return; after this method.

No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342

First of all thanks for the reply. But my question is after using this method, the response should be considered to be committed then why it is executing println statement. I know that using return; I can commit response.
Can any one please tell me what will be probable reason behind that ?

Thanks & Regards
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60810

sendRedirect does not contain any magic that alters the stream of execution of the code. All it does is to set response headers appropriately so that when the response is sent to the browser, it will perform the redirect.

It is up to you to not do anything illegal after you perform this operation.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Query on response.sendRedirect() ???
Similar Threads
include page ...problem
IllegalStateException in JSP
scwcd doubt on jsp:forward
Cannot create a session after the response has been committed
response.SendRedirect and session