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" />

 

1. Syntax of JSP include action

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" />

 

2. Attributes of JSP include action

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.

 

3. Rules & Behaviors of JSP include action

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:

JSP include standard action inclusion mechanism

Following are some rules:

4. Nested inclusion

Consider the following directory structure of a web application:

nested inclusion directory structureAssuming 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:



nested inclusion explanation

 

5. JSP include action Examples

Following are some examples that illustrate various usages of the <jsp:include> action:

<jsp:include page="content_${country}.jsp" /> 

That evaluates the variable countryto construct the page’s URL.

 

Related JSP Tutorials:

 

Other JSP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.