JSP - JSTL Core <c:import> Tag

c:import is similar to JSP 'include', which has an additional feature of using absolute URL to include the content of any resource within the server or from a different server to the current JSP page.

<c:import> Tag Syntax

Syntax:
<c:import url="relative_url"/>

<c:import> Tag Attributes

AttributeRequiredDefaultDescription
urlYesNoneThe URL of the resource to be imported
contextNoCurrent applicationThe context specifies the name of an external resource when accessing a relative resource using a relative path.
charEncodingNoISO-8859-1The character encoding used for the imported data.
varNoPrint to pageThe name of the variable to store the imported content.
scopeNoPageThe variable scope.
varReaderNoNoneOptional variables for providing java.io.Reader object.

<c:import> Tag Examples

Here is an example where it imports the content of another resource specified in the url attribute of the '<c:import>' tag. The Imported content will be stored in a variable called 'info'; then it will print on the next line using the '<c:out>' tag.

Example:
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<!DOCTYPE html>
<html>
<head>
    <title> Example of core-import tag </title>
</head>
<body>
    <c:import var="info" url="http://www.w3schools.in" charEncoding="UTF-8" />
    <c:out value = "${info}"/>
</body>
</html>
Output:

The above example will fetch the entire source code from w3schools.in and store in a variable name "info" which will eventually be printed.

If you're importing content from the same web app, you don't need a full URL; you need the URI relative to the web app root.

Example:
<c:import url="/menu.jsp" charEncoding="UTF-8" />  

Scroll Back to Top