JSTL Core c:import Tag

The <c:import> tag is used to include the content of another resource in the current JSP. The resource can be either static or dynamic. The <c:import> tag works like the but it is more flexible and powerful.

The syntax of <c:import> tag is as follows

  <c: import attributes> </c:import>

The attributes of <c:import> Tag

  • url: This is the major attribute in the tag and its mandatory attribute. This attribute is used to retrieve and import into the page. This specifies URL of the source to include.
  • var: This is optional attribute. This is used to specifies the name of the variable into which the result has to be stored, if specifies. If this is not specifies then the imported data will be printed on the current page.
  • scope: This is also optional attribute. This attribute is used to specifies the scope into which the variable has to be stored. If we are using var attribute then only we can use the scope attribute.
  • context: This attribute is used to specify the context name in which the page has to be located.

Example of <c:import> Tag

Listing 1:example.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<div style="border: 10px solid red;">
<c:import var="testHtml" url="/test.html" />
<c:out value="${testHtml}" escapeXml="false" />
<h3>test.html source code</h3>
<c:import var="testHtmlSource" url="/test.html" />
<c:out value="${testHtmlSource}" escapeXml="True" />

Html files for the above program is as follows:

Listing 2:test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Page</title>
This is the Test page

How To Run?

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server


When the execution process is completed successfully we get the following output:
JSTL_c import tag_demo

Previous Tutorial : JSTL Core c:choose,  c:when, c:otherwise Tags :: Next Tutorial : JSTL Core c:remove Tag

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!