With <c:import>, there are now THREE ways to include content
So far, we’ve used two different ways to add content from another resource into a JSP. But there’s yet another way, using JSTL.
The include directive
<%@ include file="Header.html" %>
Static: adds the content from the value of the file attribute to the current page at translation time.
The <jsp:include> standard action
<jsp:include page="Header.jsp" />
Dynamic: adds the content from the value of the page attribute to the current page at request time.
The <c:import> JSTL tag
<c:import url="http://www.wickedlysmart.com/skyler/horse.html" />Dynamic: adds the content from the value of the URL attribute to the current page, at request time. It works a lot like <jsp:include>, but it’s more powerful and flexible.
Note
Unlike the other two includes, the <c:import> url can be from outside the web Container!
Note
Do NOT confuse <c:import> (a type of include) with the “import” attribute of the page directive (a way to put a Java import statement in the generated servlet).
Note
They all have different attribute names! (And watch out for “include” vs. “import”)
Each of the three mechanisms for including content from another resource into your JSP uses a different word for the attribute. The include directive uses file, the <jsp:include> uses page, and the JSTL <c:import> tag uses url. This makes sense, when you think about it... but you do have to memorize all three. The directive was originally intended for static layout templates, like HTML headers. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access