The <jsp:include> standard action is used to include resource into the current JSP page at request time. The included resource can be static (HTMP page) or dynamic (JSP page or servlet) and must be relative to the including JSP page. For example:
<jsp:include page="content.jsp" />
The complete syntax of the <jsp:include> standard action is as follows:
<jsp:include page="relative URL" flush="true|false"/>
or:
<jsp:include page=”urlSpec” flush="true|false">
{ <jsp:param .... /> }*
</jsp:include>
The latter syntax specifies additional parameters passed to the included page using the <jsp:param> standard action:
<jsp:param name="name" value="value" />
Attribute name | Required | Description |
page | Yes | URL of the included page which is interpreted as relative to the current JSP page. The included resource can be static like HTML or dynamic like JSP and Servlet. |
flush | Optional | If set to true, the servlet container will flush the output buffer prior to the inclusion (if the page output is buffered). Default is false. |
The inclusion happens at runtime. When the servlet container encounters a <jsp:include> action, it requests the included page then adds the response into the current JSP page, where the <jsp:include> action is declared. The following picture explains the inclusion process:
Following are some rules:
Consider the following directory structure of a web application:
Assuming A.jspincludes B.jsp which includes C.jsp which includes D.jsp. The following diagram explains how the <jsp:include> action is used and how the container resolves the inclusion:
Following are some examples that illustrate various usages of the <jsp:include> action:
<jsp:include page="/header.html" /> <jsp:include page="/some/path/header.html" />
<jsp:include page="header.jsp" /> <jsp:include page="some/path/header.jsp" />
<jsp:include page="/TestServlet" />
Here, response of the servlet at the URL /TestServlet will be included to the current JSP page. Note that we use the web application’s relative path.
<jsp:include page="footer.jsp" flush="true"/>
<jsp:include page="header.jsp" > <jsp:param name="language" value="english" /> <jsp:param name="country" value="USA" /> </jsp:include>
These parameters can be accessed in the included page as follows:
Page language: ${param.language} Country: ${param.country}
<jsp:include page="content_${country}.jsp" />
That evaluates the variable countryto construct the page’s URL.