• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do we return to the redirecting servlet after response.sendRedirect

 
tarun saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. ServletB is accessed in a completely new request/response cycle, in which ServletA plays no role.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Immediately after the redirect your servlet should return from the doGet or doPost.

Bill
 
tarun saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic