The JSTL <c:import> Core Tag is used to include the content of other resource at a specific position in the current jsp. It works like include action but the difference is that it can work on relative as well as absolute url while include action can work only on relative url.
Syntax:
<c:import var="varName" url="relUrl/absUrl"/>
c:import tag attributes:
Attribute | Description | Required |
url | URL to retrieve and import into the page. | Yes |
context | / followed by the name of a local web application. | No |
charEncoding | Character set to use for imported data. | No |
var | Name of the variable to store imported text. | No |
scope | Scope of the variable used to store imported text. | No |
varReader | Name of an alternate variable to expose java.io.Reader. | No |
Example:
test.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>c:import JSTL core tag example</title> </head> <body> <c:import var="data" url="/showInfo.jsp"/> <c:out value="${data}"/> </body> </html> |
showInfo.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> This is a c:import JSTL core tag example. |
web.xml
<web-app> <welcome-file-list> <welcome-file>test.jsp</welcome-file> </welcome-file-list> </web-app> |
Output:
Download this example.
Next Topic: JSTL c:forEach Core Tag with example.
Previous Topic: JSTL c:choose , c:when and c:otherwise Core Tags with example.