文件包含指令include是JSP的另一条指令标识。通过该指令可以在一个JSP页面包含另一个JSP页面。使用include指令,最终将生成一个文件,所以在被包含和包含的文件中,不能有相同名称的变量。
include指令的语法格式如下:
<%@ include file="path"%>
该指令只有一个file属性,用于指定要包含文件的路径。该路径可以是相对路径,也可以是绝对路径。但是不可以是通过<%=%>表达式所代表的文件
案例如下:
建立一个top.jsp文件,导航条,代码如下
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<img src="image/1.png">
建立一个copy.jsp文件,尾端,代码如下
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%String copyright=" All Copyright ©出差从多角度"; %>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<table width="778" height="61" border="0" cellpadding="0"
cellspacing="0" background="image/3.png">
<tr><td><%=copyright %></td>
</tr>
</table>
</body>
</html>
建立一个inde.jsp文件,中间,代码如下
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body style="margin:0px;">
<%@ include file="top.jsp" %>
<table width="1778" height="561" border="0" cellpadding="0"
cellspacing="0" background="image/2.png">
<tr>
<td> </td>
</tr>
</table>
<%@ include file="copy.jsp" %>
</body>
</html>
运行效果如下: