JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

JSTL Core c:import Tag

February 18, 2014 by Krishna Srinivasan Leave a Comment

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

[code lang=”html”]
<c: import attributes> </c:import>
[/code]

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

[code lang=”html”]
<%@ 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" />
[/code]

Html files for the above program is as follows:

Listing 2:test.html

[code lang=”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>
[/code]

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:
JSTL_c import tag_demo

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

Filed Under: Java EE Tagged With: JSTL Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved