Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
 
Book Must Read
 
 

JSP Including Files

 
 
Print   Email
 
     
 

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">&nbsp;</td>
    <td width="65%" align="center">This is Home page and having
                    header and footer included by include of JSP </td>
    <td width="19%">&nbsp;</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>&nbsp;</td>
    <td class="st">Products</td>
    <td>&nbsp;</td>
    <td class="st">Services</td>
    <td>&nbsp;</td>
    <td class="st">Company Profile</td>
    <td>&nbsp;</td>
    <td class="st">Register</td>
    <td>&nbsp;</td>
    <td class="st">Login</td>
    <td>&nbsp;</td>
    <td class="st">About Us</td>
    <td>&nbsp;</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”

 

 

 

 
If You Like this Article, share this

 
 
     
   
  View Forum Post  
 
Optimization of SQL queries for better performance
Example of Between in mysql
Example of sorting in mysql table
Example of join in mysql
Example of subquery in mysql
union join in mysql with example
error too many connection in mysql
calculate second highest salary
How to use select queries with array in mysql
Radio button pre selected in struts
Exception in thread "main" java.lang.NoClassDefFoundError
tree view in jsp
SSL implement in tomcat and java certificate
WARNING: Internal error flushing the buffer in release()
DynaForm validation problem in struts not working
Accessing JavaBean in servlet or servlet in javaBean.
 
     
     
   
     
     
     
 
  Privacy Policy