|
The include directive is used to include any file’s content into other desired file. This process is useful when need a separate header, footer, menu bar, or any common content need to reuse on other pages.
This JSP include can be static or dynamic.
Static include in JSP does include one file content into desired file at translation time, after that JSP page is compiled.
Dynamic include in JSP
Dynamic include is used in javabean as <jsp:include>. This include execute first file and then output of this file include in desired file.
Include is useful for common and reusable code function method, variable.
Syntax for Including Files
Let take a example of include header and footer in home JSP page
home.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>Include example of JSP</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="3" align="center"><%@ include file="header.jsp"%></td>
</tr>
<tr>
<td width="16%" height="138"> </td>
<td width="65%" align="center">This is Home page and having
header and footer included by include of JSP </td>
<td width="19%"> </td>
</tr>
<tr>
<td colspan="3" align="center"><%@ include file="footer.jsp"%></td>
</tr>
</table>
</body>
</html>
Including two JSP pages in home page then we need to make two more files.
header.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>Header page</title>
<style>
.st
{
background-color:#CBE0E7;
font-weight:bold;
text-align:center
}
</style>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="st">Home</td>
<td> </td>
<td class="st">Products</td>
<td> </td>
<td class="st">Services</td>
<td> </td>
<td class="st">Company Profile</td>
<td> </td>
<td class="st">Register</td>
<td> </td>
<td class="st">Login</td>
<td> </td>
<td class="st">About Us</td>
<td> </td>
</tr>
</table>
</body>
</html>
Now footer file need to include
footer.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>Footer page</title>
</head>
<body>
<div align="center">Home || Products || Services || Login || About Us</div>
</body>
</html>
<%@ include file ="relativeURL" %>
Relative URL only can be used to include file. We can not use pathname from http protocol or port or any domain name.
No path like this
http://domainName:8080/
Only include pathname
“Test.jsp”
“Header.jsp”
“/header.jsp”
“/common/header.jsp”
|