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
Attribute | Required | Default | Description |
---|---|---|---|
url | Yes | None | The URL of the resource to be imported |
context | No | Current application | The context specifies the name of an external resource when accessing a relative resource using a relative path. |
charEncoding | No | ISO-8859-1 | The character encoding used for the imported data. |
var | No | Print to page | The name of the variable to store the imported content. |
scope | No | Page | The variable scope. |
varReader | No | None | Optional 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="//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" />