aspose file tools*
The moose likes Servlets and the fly likes Do we return to the redirecting servlet after response.sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Do we return to the redirecting servlet after response.sendRedirect" Watch "Do we return to the redirecting servlet after response.sendRedirect" New topic
Author

Do we return to the redirecting servlet after response.sendRedirect

tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
Like in RequestDispatcher.forwardTo... Can we get back to the calling servlet after response.sendRedirect

So lets say ServletA is redirecting to ServletB, so after the redirection process and the ServletB does its job, can we return to ServletA?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
No. ServletB is accessed in a completely new request/response cycle, in which ServletA plays no role.


Ping & DNS - my free Android networking tools app
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
Immediately after the redirect your servlet should return from the doGet or doPost.

Bill
tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
hey from two different replies i am confused.

So the answer is no right? I am convinced that the req, res cycle is different in sendRedirect and dispatcher.forwardTo.

if the
[code:] sysout("I am in caller servlet A");
redirection code;
sysout("I am back in A");
[:code]


The second line "I am back in A" will work only in case of dispatcher and not redirect.

Am I correct now?
in Mkumar
Greenhorn

Joined: Sep 08, 2008
Posts: 20
You can back from Servlet B to Servlet A, but request will be new one not the same request object.

-Manoj
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
The second line "I am back in A" will work only in case of dispatcher and not redirect.


Nope

Think about the request Thread of execution.

The sendRedirect causes the sending of a response back to the client, but the Thread continues executing in the original servlet. Since the response has been sent, you cant write anymore to the client.

The Thread can write to a log or clean up resources, but generally, you should just return.

By returning from the doGet or doPost you return control to the servlet container.

In the meantime the client has generated a new request to the redirected URL - that gets a completely different Thread.

Bill
tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
ok guys, now i got it.

since finally is there in the jvm stack, it has to be executed anyway unless i shut down jvm using System.exit();

but the main thing is that once the redirection takes place the request response cycle will be different and the old request/ response will not be available.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You don't want to call System.exit from a servlet app.

Simply calling return will insure that any code after the sendRedirect or forward from executing.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
 
subject: Do we return to the redirecting servlet after response.sendRedirect