This topic is the last in this course on JSP syntax and covers JSP Action tags. There are six JSP Actions:
<jsp:include/>
<jsp:forward/>
<jsp:plugin/>
<jsp:usebean/>
<jsp:setProperty/>
<jsp:getProperty/>
The <jsp:include/> lets you include the output generated
by another file into the requested JSP. The output from the included document
is inserted into the original file in place of the <jsp:include/>
tag.
<jsp:include page="URL" flush="true" /> or <jsp:include page="URL" flush="true" > <jsp:param name="ParamName1" value="ParamValue1" /> <jsp:param name="ParamName2" value="ParamValue2" /> </jsp:include>
The page attribute is an absolute or relative URL to the file whose output is to be included. (Note, it cannot contain a protocol name, port number, or domain name). The include can be a static file, another JSP, a Servlet or indeed any source that can can receive HTTP requests and generates output.
The flush attribute controls whether any current output is flushed
(sent to browser) prior to including the content from the include file. However
the JSP 1.1 specificaton is currently limited to only allow true
values. This is a limitation that may be relaxed in future specifications.
Important. The fact that we must flush the output buffer limits what
we can do with the <jsp:include/> action. In particular,
using response.sendRedirect() to another page is no longer possible.
Likewise, setting cookies or HTTP Headers will not succeed. This applies to both
the files involved: the including file and the file that is included.
As can be seen in the second syntax the include action can take parameters in
the form of name-value pairs. The action can have as many parameters as required.
The file that is being included picks up these paramters in the
same way as you would treat any request parameter: using methods such as
request.getParameter("").
As indicated in the diagram below the <jsp:include/> action
works by passing its request onto the included page, which is then handled by the
JSP Container as it would handle any other request. Because the JSP container
automatically generates and compiles new servlets for JSP pages that have changed
then if the JSP being included is changed then these changes will be automatically
reflected in the output.
Sequence diagram for the <jsp:include/> action.
In contrast the JSP include directive that we met earlier does not
automatically trigger a recompile if the included file is changed. Some JSP Containers,
JRun 3.0 for example, do check included files for changes but not all containers do.
Another feature is that JSP expressions can be used in place of the attribute values. In fact this feature is common to all JSP actions.
<jsp:include page="<%= strIncFile %>" flush="true" />
The following demo illustrates a typical use of the <jsp:include/>
action. This demo is of a ficticious newspaper - The Rangers Times and it uses the
<jsp:include/> action to include articles within the main
framework of the page.
So what do we use - the include directive or the
<jsp:include/> ?
Well both have their pros and cons and, as always, it just depends on what your
requirements are. The benefits of using the <jsp:include/> are,
The demo above is a good example because in a newspaper scenario the articles change daily and we want to avoid if possible having to recompile JSPs every day. Here the HTML article files that are included are built on a daily basis with the JSP page loading in the articles by their article ID.
One of the main benefits of using the include directive is that local page
variables can be shared between the two files. It also has slightly better run time efficiency
and doesn't restrict output buffering.
Next is a workshop that demonstrates using the <jsp:include.> action.