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:

    • The included page cannot change response status code or set headers. The servlet container will ignore those attempts.
    • The servlet container includes response (not source code) of the included page.
    • Response of the included page is inserted in to the current JSP page at the position of the <jsp:include> action (position-sensitive).
    • If URL of the included page starts with a slash (/), the servlet container will interpret the page as relative to the web application context path; if the URL starts without a slash, the container will treat the page as relative to the current JSP page (see the examples below).
    • Value of the page URL can be a runtime expression that evaluates to a String.
    • The servlet container will throw an exception if it could not find the included page.

       

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:

  • Include a static HTML page (relative to the web application’s context path):
    <jsp:include page="/header.html" />
    
    <jsp:include page="/some/path/header.html" />
  • Include a JSP page (relative to the current JSP page):
    <jsp:include page="header.jsp" />
    
    <jsp:include page="some/path/header.jsp" />
  • Include a servlet:
    <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.

  • Flush the output buffer before including:
    <jsp:include page="footer.jsp" flush="true"/>
  • Passing additional parameters:
    <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} 
  • The included page’s URL is a runtime expression:
<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 began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment