Categories: JSPTutorial

JSP include Directive

<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<p>Include Directive in JSP is an instruction from JSP to JSP Engine about including static file at Translation Time&period;<&sol;p>&NewLine;<p>The include directive is used to includes a file during the translation phase&period; This directive tells the container to merge the content of other external files with the current JSP during the translation phase&period; You may code include directives anywhere in your JSP page&period;<&sol;p>&NewLine;<p>General syntex of JSP Include Directives generally look like this&colon;<&sol;p>&NewLine;<p><b>Syntex&colon; <&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; include file&equals;"xyz" &percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>Example&colon; <&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; include file&equals;"&sol;jsp&sol;header&period;jsp" &percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>Explanation&colon; <&sol;b>As shown above&comma; include directive has only one attributes file&period;<&sol;p>&NewLine;<p><b>file Attribute&colon;<&sol;b> file attribute in Include Directive specifies the relative path of the included file&period; This file can be any html&comma; text or jsp file&period; Please be aware that included file may have html&comma; head&comma; title or any other tag that can create conflict with current JSP&period;<&sol;p>&NewLine;<p>Once the file is added statically&comma; next line in current JSP will be processed&period;<&sol;p>&NewLine;<p>A good example of include directive is including a common header and footer with multiple pages of content&period;<&sol;p>&NewLine;<p>Let us define following three files<br &sol;>&NewLine;<b>&lpar;a&rpar; header&period;jps <&sol;b><br &sol;>&NewLine;<b>&lpar;b&rpar; footer&period;jsp and <&sol;b><br &sol;>&NewLine;<b>&lpar;c&rpar; index&period;jsp <&sol;b><&sol;p>&NewLine;<p><b>Following is the content of header&period;jsp&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&excl; &NewLine; int pageCount &equals; 0&semi; &NewLine; void addCount&lpar;&rpar; &lbrace; &NewLine; pageCount&plus;&plus;&semi; &NewLine; &rcub; &NewLine;&percnt;&gt&semi; &NewLine;&lt&semi;&percnt; addCount&lpar;&rpar;&semi; &percnt;&gt&semi; &NewLine;&lt&semi;html&gt&semi; &NewLine;&lt&semi;head&gt&semi; &NewLine;&lt&semi;title&gt&semi;The include Directive Example&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine;&lt&semi;body&gt&semi; &NewLine;&lt&semi;center&gt&semi; &NewLine;&lt&semi;h2&gt&semi;The include Directive Example&lt&semi;&sol;h2&gt&semi; &NewLine;&lt&semi;p&gt&semi;This site has been visited &lt&semi;&percnt;&equals; pageCount &percnt;&gt&semi; times&period;&lt&semi;&sol;p&gt&semi; &NewLine;&lt&semi;&sol;center&gt&semi; &NewLine;&lt&semi;br&sol;&gt&semi;&lt&semi;br&sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>footer&period;jsp<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;br&sol;&gt&semi;&lt&semi;br&sol;&gt&semi; &NewLine;&lt&semi;center&gt&semi; &NewLine;&lt&semi;p&gt&semi;Copyright © 2013 dineshonjava&period;com&lt&semi;&sol;p&gt&semi; &NewLine;&lt&semi;&sol;center&gt&semi; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>index&period;jsp<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; include file&equals;"header&period;jsp" &percnt;&gt&semi; &NewLine;&lt&semi;center&gt&semi; &NewLine;&lt&semi;p&gt&semi;Thanks for visiting my page&period;&lt&semi;&sol;p&gt&semi; &NewLine;&lt&semi;&sol;center&gt&semi; &NewLine;&lt&semi;&percnt;&commat; include file&equals;"footer&period;jsp" &percnt;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>Now these three files copied to following folder and restart server&period;<br &sol;>&NewLine;<b>C&colon;Program Files &lpar;x86&rpar;Apache Software FoundationTomcat 7&period;0webappsindex<&sol;b><&sol;p>&NewLine;<p>Hit following url&colon;<br &sol;>&NewLine;<i><b>http&colon;&sol;&sol;localhost&colon;8080&sol;index&sol;index&period;jsp<&sol;b><&sol;i><&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;09&sol;222222&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;taglib-directive-in-jsp&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-tutorial-baby-step-to-server-pages&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-forward-tag-example&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;taglib-directive-in-jsp&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-forward-tag-example&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '343'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago