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.

  1. 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.

  2. 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.

  3. 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. ...

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.