Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are several mechanisms for reusing content in a JSP file.</p> <p>The following <strong>4 mechanisms to include content in JSP</strong> can be categorized as <strong>direct reuse</strong>:<br> (for the first 3 mechanisms quoting from <a href="https://rads.stackoverflow.com/amzn/click/0596516681" rel="noreferrer">"Head First Servlets and JSP"</a>)</p> <blockquote> <h2>1) The include directive:</h2> <pre><code>&lt;%@ include file="header.html" %&gt; </code></pre> <p><strong>Static</strong>: adds the content from the value of the file attribute to the current page <strong>at translation time</strong>. The directive was originally intended for static layout templates, like HTML headers.</p> <h2>2) The <code>&lt;jsp:include&gt;</code> standard action:</h2> <pre><code>&lt;jsp:include page="header.jsp" /&gt; </code></pre> <p><strong>Dynamic</strong>: adds the content from the value of the page attribute to the current page <strong>at request time</strong>. Was intended more for dynamic content coming from JSPs. <h2>3) The <code>&lt;c:import&gt;</code> JSTL tag:</h2></p> <pre><code>&lt;c:import url=”http://www.example.com/foo/bar.html” /&gt; </code></pre> <p><strong>Dynamic</strong>: adds the content from the value of the URL attribute to the current page, <strong>at request time</strong>. It works a lot like <code>&lt;jsp:include&gt;</code>, but it’s more powerful and flexible: unlike the other two includes, the <code>&lt;c:import&gt;</code> <strong>url can be from outside the web Container</strong>!</p> <h2>4) Preludes and codas:</h2> <p><strong>Static</strong>: preludes and codas <strong>can be applied only to the beginnings and ends of pages</strong>.<br> You can implicitly include <em>preludes</em> (also called headers) and <em>codas</em> (also called footers) for a group of JSP pages by adding <code>&lt;include-prelude&gt;</code> and <code>&lt;include-coda&gt;</code> elements respectively within a <code>&lt;jsp-property-group&gt;</code> element in the Web application <em>web.xml</em> deployment descriptor. Read more here:<br> • <a href="https://docs.oracle.com/cd/E24329_01/web.1211/e21049/configurejsp.htm#WBAPP188" rel="noreferrer">Configuring Implicit Includes at the Beginning and End of JSPs</a><br> • <a href="http://docs.oracle.com/javaee/5/tutorial/doc/bnajg.html#bnajl" rel="noreferrer">Defining implicit includes</a> </p> </blockquote> <hr> <p><strong>Tag File</strong> is an <strong>indirect method</strong> of content reuse, the way of <strong>encapsulating reusable content</strong>. A <em>Tag File</em> is a source file that contains a fragment of JSP code that is reusable as a <strong>custom tag</strong>. </p> <p>The PURPOSE of includes and Tag Files is different.</p> <p><a href="http://www.softwaresummit.com/2005/speakers/BergmanJSP2.0TagFiles.pdf" rel="noreferrer"><strong>Tag file</strong></a> (a concept introduced with JSP 2.0) is one of the options <strong>for creating custom tags</strong>. It's a faster and easier way to build <em>custom tags</em>. <a href="http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html" rel="noreferrer"><strong>Custom tags</strong></a>, also known as tag extensions, are JSP elements that allow custom logic and output provided by other Java components to be inserted into JSP pages. The logic provided through a custom tag is implemented by a Java object known as a <em>tag handler</em>. </p> <p>Some examples of tasks that can be performed by custom tags include operating on implicit objects, processing forms, accessing databases and other enterprise services such as email and directories, and implementing flow control. </p> <hr> <h3>Regarding your Edit</h3> <p>Maybe in your example (in your <em>Edit</em>), there is no difference between using direct include and a Tag File. But <strong>custom tags have a rich set of features</strong>. They can</p> <ul> <li><p>Be customized by means of attributes passed from the calling page.</p></li> <li><p>Pass variables back to the calling page.</p></li> <li><p>Access all the objects available to JSP pages.</p></li> <li><p>Communicate with each other. You can create and initialize a JavaBeans component, create a public EL variable that refers to that bean in one tag, and then use the bean in another tag.</p></li> <li><p>Be nested within one another and communicate by means of private variables.</p></li> </ul> <p>Also read this from "Pro JSP 2": <a href="http://books.google.ee/books?id=A3FuJXgx0RAC&amp;lpg=PA256&amp;ots=hqQMmIx_cH&amp;dq=usage%20of%20jsp%20tag%20files&amp;pg=PA252#v=onepage&amp;q=usage%20of%20jsp%20tag%20files&amp;f=false" rel="noreferrer">Understanding JSP Custom Tags</a>.</p> <hr> <h2>Useful reading.</h2> <ul> <li><p><a href="http://javarevisited.blogspot.com/2012/01/difference-between-page-include-and.html" rel="noreferrer"><strong>Difference between include directive and include action in JSP</strong></a></p></li> <li><p><a href="https://stackoverflow.com/a/3257426/814702"><strong>JSP tricks to make templating easier</strong></a></p></li> <li><p>Very informative and easy to understand tutorial from <em>coreservlet.com</em> with beautiful explanations that include <code>&lt;jsp:include&gt; VS. &lt;%@ include %&gt;</code> comparison table:<br> <a href="http://www.java-programming.info/tutorial/pdf/csajsp2/12-File-Inclusion.pdf" rel="noreferrer"><strong>Including Files and Applets in JSP Pages</strong></a></p></li> <li><p>Another nice tutorial from <em>coreservlets.com</em> related to tag libraries and tag files:<br> <a href="http://courses.coreservlets.com/Course-Materials/pdf/msajsp/07-Basic-Custom-Tags.pdf" rel="noreferrer"><strong>Creating Custom JSP Tag Libraries: The Basics</strong></a></p></li> <li><p>The official Java EE 5 Tutorial with examples:<br> <a href="http://docs.oracle.com/javaee/5/tutorial/doc/bnama.html" rel="noreferrer"><strong>Encapsulating Reusable Content Using Tag Files</strong></a>. </p></li> <li><p>This page from the official Java EE 5 tutorial should give you even more understanding:<br> <a href="http://docs.oracle.com/javaee/5/tutorial/doc/bnajb.html" rel="noreferrer"><strong>Reusing Content in JSP Pages</strong></a>.</p></li> <li><p>This excerpt from the book "Pro JSP 2" also discuses <strong>why do you need a Tag File instead of using static include</strong>:<br> <a href="http://books.google.ee/books?id=A3FuJXgx0RAC&amp;lpg=PA256&amp;ots=hqQMmIx_cH&amp;dq=usage%20of%20jsp%20tag%20files&amp;pg=PA257#v=onepage&amp;q&amp;f=false" rel="noreferrer"><strong>Reusing Content with Tag Files</strong></a></p></li> </ul> <hr> <h2>Conclusion</h2> <p><em>Use the right instruments for the concrete task.</em></p> <p><strong>Use <em>Tag Files</em></strong> as a quick and easy way of creating <em>custom tags</em>. </p> <p>As for the including content in JSP (quote from <a href="http://sharat.wordpress.com/2006/08/28/10-what-is-the-difference-between-pagedirective-include-action-tag-include/" rel="noreferrer">here</a>):</p> <blockquote> <ul> <li><p><strong>Use the include directive</strong> if the file changes rarely. It’s the fastest mechanism. If your container doesn’t automatically detect changes, you can force the changes to take effect by deleting the main page class file.</p></li> <li><p><strong>Use the include action</strong> only for content that changes often, and if which page to include cannot be decided until the main page is requested.</p></li> </ul> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload