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. In other words, a “file”. The <jsp:include> was intended more for dynamic content coming from JSPs, so they named the attribute “page” to reflect that. The attribute for <c:import> is named for exactly what you give it—a URL! Remember, the first two “includes” can’t go outside the current Container, but <c:import> can.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset