Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward() can be include()...

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
include() is temporary hand-off but forward() is also not permanent. I mean, control (execution) will anyway come to one who forwarded the request so ultimately it's same as include() (giving request and response to somebody else for some time)...

 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I mean, control (execution) will anyway come to one who forwarded the request so ultimately it's same as include() (giving request and response to somebody else for some time)...


Yes it will. Servlet code is still java code and forward being just a method call, the control would return to the callee after the call is over. But that doesnt however mean the servlet can take advantage of this and continue processing. Infact an exception would be thrown if you have Servlet code that attempts to do so. Which is why it is best to return from the method immediately after a forward (and a redirect).

ram.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not call them the same in therm that they are interchangeable, they are not. In limitted cases they can behave the same, but their intent is different.

'include' allows you join together several output parts into a single response.'forward' will not allow you to send anything before or afterwards (unless it can be cleared from the cache first)

Therefore if you want to allow multiple parts in your response, you must use 'include', if you want to ensure response X is sent you must use 'forward'.

Dave
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramprasad madathil:


Yes it will. Servlet code is still java code and forward being just a method call, the control would return to the callee after the call is over. But that doesnt however mean the servlet can take advantage of this and continue processing. Infact an exception would be thrown if you have Servlet code that attempts to do so. Which is why it is best to return from the method immediately after a forward (and a redirect).

ram.


Then, why to use forward()???

:roll:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to transfer the request to another resource, use forward. If you want to include the output of that other resource, use include. What's the confusion?
[ June 27, 2006: Message edited by: Bear Bibeault ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
If you want to transfer the request to another resource, use forward. If you want to include the output of that other resource, use include. What's the confusion?

[ June 27, 2006: Message edited by: Bear Bibeault ]



My confusion is that, both are methods, so both will do something and return control to caller.

In both the cases, we go to any other JSP or servlet (after forward() or include()), and do something.

Now, in include() also, we can write the code, which we write in forward(). In addition, we have flexibility to modify something in caller, later (after completion of include())...

Then why to use forward()???



Thanks a lot.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because even though they both return to the caller -- all Java methods return to the caller, why is that an issue? -- they act completely differently.

forward() will transfer the current request to another resource, and include() will create a new sub-request whose response is included in the parent response.

You can use a screw-driver to bang in a nail if you whack it hard enough, but it's best to use the appropriate tool for the job.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic