Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JSP and the fly likes JSP include vs include directive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP include vs include directive" Watch "JSP include vs include directive" New topic

JSP include vs include directive

Nigel King

Joined: Mar 03, 2006
Posts: 29
If I have a JSP page which carries out a database query and returns a drop-down box of all the returned results, should I <jsp:include> this or just <include> it?
Obviously the returned HTML will be different every time. Does that hint at <jsp:include> ? Is <include> just for jsp pages that return the same HTML every time?
Hope someone can help clear this up for me.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
As your JSP code is not going to change every time, you can use JSP inlucde and no need to use include directive.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Filling a drop-down list with data dynamically SELECTED from a database doesn't imply the *need* to use an include of either type. Can you explain why you think you need one?

I use including to break pages into logical pieces and to reuse sections on multiple pages.

How do you choose between the two kinds of includes? One difference that everyone mentions is *when* the including happens. The jsp:include action is evaluated at request time, so if the included file has been editted, this is detected and it will be recompiled. On the other hand, the include directive <%@ include file="" %> is evaluated when the page containing the directive is compiled -- it's essentially a copy-and-paste operation, so that later changes to the included file may not be detected.

Either of these would work for the original poster if the fragment being included is jsp that loads data using select from his database. Don't confuse compiling with running this code! The code will always be run at request time.

All this being said, I use the jsp:include action almost exclusively, and not for the "editting the included file" reason. I think that the include directive leads to code bloat, if the fragment is pasted in multiple places. The include action has a small runtime overhead, but that is nothing to worry about.

Finally, there is one place where you have to always use the include directive. If you are putting all your taglib directives in a single file, you'll have to use the include directive to use that fragment:

<%@ include file="taglibs.jspf" %>

There is no emoticon for what I am feeling!
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

There is an entry in the JSP Faq section that explains the difference between the include directive and the include action.
It might help you.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nigel King

Joined: Mar 03, 2006
Posts: 29
Thanks for all your replies.
I'm building a web site - much as Jeff described - and trying to keep it as modular as possible. So all the pages are actually lots of JSPs all <jsp:included> together.
My understanding is something like this then ...
if I <jsp:include> my DropDown.jsp, I can upload a new DropDown.jsp and it will be detected and incorporated.
However, if I were to <include> it, an upload will not necessarily be detected and the changes might not be visible until the parent JSP is retranslated.
If that's the case, and the overhead is minimal, I'll stick to <jsp:include>
David Kago

Joined: Mar 16, 2006
Posts: 1
Try using tiles - very effective and can give you the modular results you are looking for.
anand rathna

Joined: Jul 04, 2008
Posts: 4
as far as I know, includes the content and then compiles the whole jsp, whereas compiles the included jsp and inserts only the html content. Hope this helps and not confuses
mak pandian
Ranch Hand

Joined: Jul 06, 2011
Posts: 30

As far as I know, In terms of performance, Standard include action would be better than directive include. Because executing a big JSP file in a single shot would take more time and memory. Am I right?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

mak pandian wrote:As far as I know, In terms of performance, Standard include action would be better than directive include. Because executing a big JSP file in a single shot would take more time and memory. Am I right?

Probably not. But regardless, supposed performance and memory micro-optimization should not be use to determine which approach to take. Use whichever one makes the most sene, and is most suited to the task.

Avoid premature optimization.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: JSP include vs include directive
It's not a secret anymore!