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>&lt;c:import&gt; Demo</title>
    </head>
    <body>
      <h1>&lt;c:import&gt; 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:

c-import-demo

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


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#1Darshan2020-08-07 05:25
how to handle json response using C:import any idea
Quote