In this example you can learn how to include a JSP fragment into another JSP page. This is a common practice when creating a web application where we usually have a navigation section, the main content and the footer of a web page. Using the include
directive make it simpler to maintain the fragment of a web page, which mean that when we need to change for example the footer section we just need to alter the footer include file and all the page that includes it will get the benefit.
The page inclusion that using the include
direction will occur at page translation time, it is when the JSP page is translated into a Servlet by JSP container. We can use any file extension name for the JSP fragment used by the include
directive. In this example we use the .jspf
extension which is short for JSP Fragment.
Here is an example of JSP with include
directive.
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JSP - Include Directive</title>
</head>
<body>
<div id="header">
<%@ include file="/include/common/header.jspf" %>
</div>
<div id="content">
Main application content goes here!
</div>
<div id="footer">
<%@ include file="/include/common/footer.jspf" %>
</div>
</body>
</html>
header.jspf
fragment.
Header
<hr/>
footer.jspf
fragment.
<hr/>
Footer
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023
Hi,
Thanks for your codes, it’s really helpful. With your respected permission I want to add something for your this code. If you will include JSP file,
<%@ include file=""%>
provides you a static include. A static include is resolved at compile time, and may thus not use a parameter value, which is only known at execution time.If you want to pass parameters to child JSP files, you can use
<jsp:include page=""/>
.Have good coding 🙂
Thanks for the heads up!