File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Why are codes after request.getRequestDispatcher still executed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Why are codes after request.getRequestDispatcher still executed?" Watch "Why are codes after request.getRequestDispatcher still executed?" New topic
Author

Why are codes after request.getRequestDispatcher still executed?

Aloysius Lim
Greenhorn

Joined: Jun 04, 2012
Posts: 3
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!




[Thumbnail for qn.png]

E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aloysius Lim
Greenhorn

Joined: Jun 04, 2012
Posts: 3
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

Joined: Oct 27, 2005
Posts: 19679
    
  18

As said before, forwarding the request does not exit the method. The rest of the method will still be executed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

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

Joined: Jun 04, 2012
Posts: 3
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why are codes after request.getRequestDispatcher still executed?