• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do we return to the redirecting servlet after response.sendRedirect

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. ServletB is accessed in a completely new request/response cycle, in which ServletA plays no role.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic