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 the including page is translated. So if you’re pretty sure that once you go to production the included file won’t change, the directive might be the way to go. Of course there’s still the tradeoff that the generated servlet class is a little larger when you use the directive.

Q:

Q: I tried this with Tomcat— I made a static HTML file, and included it with the directive. Then I changed the HTML file, without redeploying or anything, and the output from the JSP reflected the difference! So if that’s the case, then why ever use <jsp:include >?

A:

A: Ahhh... you have a friendly Container (like Tomcat 5). Yes, most of the newer Containers have a way of detecting when the included files have changed, and they do retranslate the including file and everything’s great. The problem is that this is NOT GUARANTEED BY THE SPEC! So if you write your code to depend on it, your app won’t necessarily be portable to other Containers.

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

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