The following code example shows a Program to Demonstrate include Directive in JSP.
Here is an example of using the include
directive in JSP to display the current date from another JSP page.
DatePage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Date Page</title>
</head>
<body>
<h2>Current Date:</h2>
<%= new java.util.Date() %>
</body>
</html>
MainPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Main Page</title>
</head>
<body>
<h2>Main Page</h2>
<jsp:include page="DatePage.jsp"/>
</body>
</html>
In this example, DatePage.jsp
is a separate JSP page that displays the current date. The MainPage.jsp
includes this page using the <jsp:include>
directive and specifies the path to the DatePage.jsp
file using the page
attribute.
When MainPage.jsp
is executed, the content of DatePage.jsp
is included and the current date is displayed on the page. This allows you to reuse the code for displaying the current date in multiple pages.