<%@ include file="content.html" %>
<%@ include file="relative URL" %>
XML syntax:<jsp:directive.include file="relative URL"/>
The included file’s URL can be relative to the web application’s context path or relative to the current JSP page.
Following are some rules:<%@ include file="content.html" %> <%@ include file="some/path/content.html" %> <jsp:directive.include file="content.html"/>
<%@ include file="/content.html" %> <%@ include file="/some/path/content.html" %> <jsp:directive.include file="/content.html"/>Let’s see a common example in which a main JSP page includes two files header and footer. Because header and footer are common to all pages so it’s recommended to separate them into two files which can be reused across the main pages.Code of the home.jsppage:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home Page</title>
</head>
<body>
<%@include file="header.html" %>
<hr/>
<h2>This is main content</h2>
<hr/>
<%@include file="footer.html" %>
</body>
</html> Code of the header.htmlpage:<h1>This is header</h1>Code of the footer.htmlpage:
<h5>This is footer</h5>Output when running the home.jsp page:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.