File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes forward() VS include() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "forward() VS include()" Watch "forward() VS include()" New topic

forward() VS include()

Yog Ananth

Joined: Dec 28, 2004
Posts: 27
Please explain what is the difference between forward() and include() in RequestDispatcher.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

They do exactly as their names suggest.

The descriptions in the javadocs are pretty good.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
The one key difference between forward() and include() is the fact that forward() clears the output buffer prior to forwarding control to the other servlet (JSPs are servlets ultimately). So, DO NOT forward to another servlet after you've written data to the output stream as the response may already be committed. The include() method merely includes the output of the other servlet inline.

James Carman, President<br />Carman Consulting, Inc.
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Thank you James.
I agree. Here's the link:
subject: forward() VS include()
jQuery in Action, 3rd edition