Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why are codes after request.getRequestDispatcher still executed?

 
Aloysius Lim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new to JSP. I am trying to learn more on response.sendRedirect and requestDispatcher.forward.
While playing around with some codings, I got puzzled over why codes after requestDispatcher.forward are still executed.

I have included my codes and output in the image attached. Could someone point to me why the 4 lines of System.out.println() are executed after
requestDispatcher.forward?

//Edited
I tried replacing requestDispatcher.forward with response.sendRedirect. And the println() are still executed. So, I'm assuming regardless of whether is it
.forward or .sendRedirect, control is always returned to the callee? I tried searching for answers but it seems like the answers given by others vary too !
Now I'm getting more confused! Ha :)

Anyway, is there a way that the control would not be returned to the callee ?

Thanks in advance!

qn.png
[Thumbnail for qn.png]
Output of requestDispatcher.forward
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aloysius Lim wrote:Hi everyone, I'm new to JSP. I am trying to learn more on response.sendRedirect and requestDispatcher.forward.
While playing around with some codings, I got puzzled over why codes after requestDispatcher.forward are still executed.

I have included my codes and output in the image attached. Could someone point to me why the 4 lines of System.out.println() are executed after
requestDispatcher.forward?

Thanks in advance!


Disclaimer:I didn't download and read your attachments.

Servlets are still java code and so the rules for blocks of code that gets executed still follow normal java rules.
There are also times when some people may want to add other useful operations after sendRedirect (e.g logging the fact or collecting statistics and navigation history)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use attachments. Many people, myself included, will not download them. Cut and paste the text, and be sure to UseCodeTags.

And, as pointed out, servlets aren't magic. The better question would be, how would it be possible that those lines not execute?
 
Aloysius Lim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Don't use attachments. Many people, myself included, will not download them. Cut and paste the text, and be sure to UseCodeTags.

And, as pointed out, servlets aren't magic. The better question would be, how would it be possible that those lines not execute?


Wouldn't the control be forwarded to the specified JSP ?
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said before, forwarding the request does not exit the method. The rest of the method will still be executed.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aloysius Lim wrote:Wouldn't the control be forwarded to the specified JSP ?

You are just calling a method. And methods return control to their caller. Why should this method be any different?
 
Aloysius Lim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:As said before, forwarding the request does not exit the method. The rest of the method will still be executed.


I see, thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic