JSTL Core Tag c:import Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
In this post, you will learn how to use the <c:import> tag in the JSTL core tags library with code examples.
Using <c:import> we can include the contents of an external web site or from any file in the relative path.
JSTL <c:import> Syntax:
<c:import
url="<string>"
var="<string>"
scope="<string>"
varRender="<string>"
context="<string>"
charEncoding="<string>"/>
Attributes:
Name | Required | Type | Description |
url | True | java.lang.String | The URL of the web page or local file to import. |
var | False | java.lang.String | Name of the variable which holds the imported content. |
scope | False | java.lang.String | Scope of the variable to store imported content. Default is Page |
varRender | False | java.lang.String | Name of the variable to expose the imported content as Reader. This variable is of type java.io.Reader. |
context | False | java.lang.String | Web application context information when accessing a relative resource that belongs to an external context. |
charEncoding | False | java.lang.String | Character encoding of the imported content. |
JSTL <c:import> Example:
The below is full example where we can get any URL provided by the user and using the <c:import> we can import the contents into the current page:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!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><c:import> Demo</title> </head> <body> <h1><c:import> Demo</h1> <form name="importForm" action="${pageContext.request.contextPath}/tag-types/core/import.jsp" method="POST"> Enter URL to import into this page: (Eg: http://www.codejava.net)<br/> <input type="text" name="urlToImport" size="50"/> <input type="Submit" value="Import"/> </form> <c:catch var="notAValidURL"> <c:import var="webData" url="${param.urlToImport}"/> <c:out value="${webData}" escapeXml="false"/> </c:catch> </body> </html>
<c:import var="staticData" url="/static.html"/> <c:out value="${staticData}" escapeXml="false"/>
The above example imports a static file and outputs its content to standard output. Note that the resource static.html is relative to the JSP page being invoked.
<c:import var="webData" url="http://www.google.com"/> <c:out value="${webData}" escapeXml="false"/>
In the above example we’re importing an external resource specified by a full URL.
Output:
The above screen shows how any URL can be imported using the <c:import> tag.
Recommended Usage of JSTL <c:import> tag:
The main advantage of using the <c:import> is that we can include the contents of an external website in the current page as well as resources from within the web application.
Other JSTL Core Tags:
if | catch | choose | forEach | forTokens | out | param | redirect | remove | set | url
Comments