wood burning stoves 2.0*
The moose likes Servlets and the fly likes Include Action vs. Directive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Include Action vs. Directive" Watch "Include Action vs. Directive" New topic
Author

Include Action vs. Directive

Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
More on my experience with both types of include:
The directive: <%@ include file="myFile.jsp" %>
versus the action:
<jsp:include page="myFile.jsp" flush="true"/>
At work, I am developing an Intranet application which pages always displays three "frames" (not real frames): a left one (15%), the main center one (70%) and the right one (also 15%).
At first I used the include action which guarantees that the JSPs included are always compiled anew when it is updated independently of whether the including page was changed.
This is a slower mechanism due to the fact that THREE servlets (one for each JSP) are involved in the chaining of the server response. You can see, the first time when all three JSPs have to be compiled, the appearance of first the left "frame", then the middle one and finally the third one.
That's because there are three servlets involved in sending the response due to the include action.
With the directive, since it is a compile-time include, there is only ONE servlet (JSP) involved and the response appears in the browser all at once.
A disadvantage is that if there is a bug in one of the JSP pages, with the include action, only that page is affected, resulting in a good two other frames and the bad one with a "500" type error.
With the directive, since it's only ONE JSP that gets compiled, if any of its constituent (included) parts has an error, the WHOLE page has an error.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Include action and directive solve different problem domains (although there is some overlap).
The include directive is the equivalent of the #include statement of the C preprocessor -- if you find there are bits of (set-up) code repeated in several pages, if you want to isolate assumptions, or if you want to split a large and complex page into smaller components, you can use this.
Arguably, it is an over-used throwback to an earlier style of programming. Object-oriented it is not. Alternatives could be ordinary Java objects, beans, taglibs, and superclasses (remember you can define your JSP to extend a custom class).
If you're using the include directive to split your page into components, consider that these components might be re-usable. It might be worth generalising them, creating jsps that generate complete and self-contained parts of a page. The include [EM]action[/EM] and parameters can be used to drive these components. This is an especially powerful technique if
- You want to be able to rapidly assemble new pages (or even complete projects) out of "page component libraries" built up during the development previous pages/projects.
- The pages are user customisable (a la Yahoo) -- rather than having one huge JSP that contains every bit of possible content, you have the individual components as self-contained jsps and dynamically include them.
There is obviously a performance hit to include actions. For an intranet this is generally not a problem but it may not be appropriate for a high volume website.
- Peter

[This message has been edited by Peter den Haan (edited January 21, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Include Action vs. Directive