- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Please share a running example of include directive in JSP
The include directive is used to include a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. You may code include directives anywhere in your JSP page.
The general usage form of this directive is as follows −
<%@ include file = "relative url" >
The filename in the include directive is actually a relative URL. If you just specify a filename with no associated path, the JSP compiler assumes that the file is in the same directory as your JSP.
You can write the XML equivalent of the above syntax as follows −
<jsp:directive.include file = "relative url" />
Example
A good example of the include directive is including a common header and footer with multiple pages of content.
Let us define the following three files (a) header.jps, (b)footer.jsp, and (c)main.jsp as follows −
Following is the content of header.jsp −
Example
<%! int pageCount = 0; void addCount() { pageCount++; } %> <% addCount(); %> <html> <head> <title>The include Directive Example</title> </head> <body> <center> <h2>The include Directive Example</h2> <p>This site has been visited <%= pageCount %> times.</p> </center> <br/><br/>
Following is the content of footer.jsp −
<br/><br/> <center> <p>Copyright © 2010</p> </center> </body> </html>
Finally here is the content of main.jsp −
<%@ include file = "header.jsp" %> <center> <p>Thanks for visiting my page.</p> </center> <%@ include file = "footer.jsp" %>
Let us now keep all these files in the root directory and try to access main.jsp. You will receive the following output −
The include Directive Example This site has been visited 1 times. Thanks for visiting my page. Copyright © 2010
Refresh main.jsp and you will find that the page hit counter keeps increasing.
You can design your webpages based on your creative instincts; it is recommended you keep the dynamic parts of your website in separate files and then include them in the main file. This makes it easy when you need to change a part of your webpage.
- Related Articles
- Please share one example of using taglib directive in JSP
- I am facing problem in using include directive tag in jsp. Please share a working example.
- Please share a working example of session maintenance in JSP.
- What is a include directive in JSP?
- What is the difference between include action and include directive in JSP?
- I need to understand how to use a bean and update its properties in JSP page. Please share an example.
- Please explain lifecycle of a JSP
- What is a page directive in JSP?
- What is a taglib directive in JSP?
- What is the purpose of taglib directive in JSP?
- What are various attributes Of page directive in JSP?
- How many types of directive tags JSP supports?
- Running the collection using Newman through share link in Postman
- What is the use of page object in JSP? Need an example.
- Can we calculate average speed in a distance-time-graph which is in non-uniform motion? If yes, please give an example.
