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" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ 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" %> <h3>test.html</h3> <div style="border: 10px solid red;"> <c:import var="testHtml" url="/test.html" /> <c:out value="${testHtml}" escapeXml="false" /> </div> <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
</pre> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Test Page</title> </head> <body> This is the Test page </body> </html>
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
Output
When the execution process is completed successfully we get the following output:
Previous Tutorial : JSTL Core c:choose, c:when, c:otherwise Tags :: Next Tutorial : JSTL Core c:remove Tag