The include directive happens at translation time <jsp:include> happens at runtime
With the include directive, there is NO difference between you opening your JSP page and pasting in the contents of “Header.jsp”. In other words, it really is just as though you duplicated the code from the header file into your other JSP. Except the Container does it at translation time for you, so that you don’t have to duplicate the code everywhere. You can write all your pages with an include directive, and the Container will go through the trouble of copying the header code into each JSP before translating and compiling the generated servlet.
But <jsp:include> is a completely different story. Rather than copying in the source code from “Header.jsp”, the include standard action inserts the response of “Header.jsp”, at runtime. The key to <jsp:include> is that the Container is creating a RequestDispatcher from the page attribute and applying the include() method. The dispatched/included JSP executes against the same request and response objects, within the same thread.
Note
The include directive inserts the SOURCE of “Header.jsp”, at translation time.
But the <jsp:include /> standard action inserts the RESPONSE of “Header.jsp”, at runtime.
Q: | Q: So why wouldn’t you always use <jsp:include>? That way you can guarantee you’ll always have the latest content. |
A: | A: Think about it. There’s an extra performance hit with every <jsp:include>. With the directive, on the other hand, the hit happens only once—when ... |
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