File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes forward() can be include()... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "forward() can be include()..." Watch "forward() can be include()..." New topic
Author

forward() can be include()...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jan 24, 2005
Posts: 489


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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward() can be include()...
 
Similar Threads
forward--RequestDispatcher
RequestDispatcher for including static contents
RequestDispatcher doubt :(
RequestDispatcher
JSP