Difference between Forward and Include

What's the Difference between Forward and Include?

The <jsp:forward> action enables you to forward the request to a static HTML file, a servlet, or another JSP.

<jsp:forward page="url" />

The JSP that contains the <jsp:forward> action stops processing, clears its buffer, and forwards the request to the target resource. Note that the calling JSP should not write anything to the response prior to the <jsp:forward> action.

You can also pass additional parameters to the target resource using the <jsp:param> tag.

<jsp:forward page="test.htm" >
   <jsp:param name="name1" value="value1" />
   <jsp:param name="name2" value="value2" />
</jsp:forward>

In this example, test.jsp can access the value of name1 using request.getParameter("name1").

To "include" another resource with a JSP, you have two options: the include directive and the include action.

The include directive executes when the JSP is compiled, which parses any JSP elements in the included file for a static result that is the same for every instance of that JSP. The syntax for the include directive is <@ include file="some-filename" %>.
The include action, on the other hand, executes for each client request of the JSP, which means the file is not parsed but included in place. This provides the capability to dynamically change not only the content that you want to include, but also the output of that content. The syntax for the include action is <jsp:include page="some-filename" flush="true" />. Note that the flush attribute must always be included (in JSP 1.1) to force a flush of the buffer in the output stream.

You can also pass parameters to the included file using the same process as the <jsp:forward> action:

<jsp:include page="template.htm" flush="true" >
   <jsp:param name="name1" value="value1" />
</jsp:include>

Remember that template.jsp can access the value of name1 using request.getParameter("name1").

Venkatesh

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.