This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP include vs include directive

 
Nigel King
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" %>
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

http://faq.javaranch.com/view?IncludesActionDirective
 
Nigel King
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using tiles - very effective and can give you the modular results you are looking for.
 
anand rathna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic