• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why are codes after request.getRequestDispatcher still executed?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic