JSP include action examples
- Details
- Written by Nam Ha Minh
- Last Updated on 22 July 2019   |   Print Email
<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: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:
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:
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:
- Summary of EL operators with examples
- Java Servlet and JSP Hello World Tutorial with Eclipse, Maven and Apache Tomcat
- How to handle exceptions in JSP
- How to list records in a database table using JSP and JSTL
- How to create dynamic drop down list in JSP from database
- Sending e-mail with JSP, Servlet and JavaMail
Comments