• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP include vs include directive

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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" %>
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using tiles - very effective and can give you the modular results you are looking for.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic