JSTL <c:import> tag is used for importing the content from another file/page to the current JSP page.
Syntax:
<c:import var="variable_name" url="relative_url"/>
Here variable_name is a variable which stores the data imported from another url.
relative_url is the address of the file/page which needs to be imported.
Attributes of <c:import>
- url: It’s mandatory attribute and needs to be mentioned always.
- var: It is an optional attribute if this is not specified then the imported data will be printed on the current page. For e.g. the statement <c:import url=”/file.jsp” /> would print the data of file.jsp on the client (browser).
- scope: It is also optional. If we are using var attribute then scope can be used along with it to specify the scope of the data stored in the variable.
Example
This is an page which has some data. We will import this page in index.jsp page.
display.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:out value="Chaitanya"/> <c:out value="BeginnersBook.com" /> <c:out value="This is just a String" />
index.jsp
Here we are importing the data from display.jsp into a variable mydata and then we are displaying it on browser using <c:out> tag.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title> JSTL c:import Tag Example</title> </head> <body> <c:import var="mydata" url="/display.jsp"/> <c:out value="${mydata}"/> </body> </html>
Output Screen:
Leave a Reply