O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required