日期:2014-05-16  浏览次数:20329 次

JSP中的静态包含和动态包含

接触过asp.net的都知道它里面有母版这么一种东东,而jsp又跟asp如此相似,那么jsp中也应该有类似的东西,它就是包含。JSP中有两种包含:静态包含:<%@include file="被包含页面"%>和动态包含:<jsp:include page="被包含页面"flush="true">。下面以一个例子来说明如何使用包含。


实例:

项目文件树:

header.jsp文件中内容:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<h1>This is header</</h1>

content.jsp文件中内容:
<table border="1">
	<tr>
		<td>col1</td>
		<td>col2</td>
	</tr>
	<tr>
		<td>col1</td>
		<td>col2</td>
	</tr>
</table>

footer.jsp文件中内容:
<hr>
copyright: 1999-2010
</body>
</html>

静态包含:

index.jsp文件中内容:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ include file="/header.jsp" %>
<%@ include file="/content.jsp" %>
<%@ include file="/footer.jsp" %>
页面显示结果:


被编译成的java文件: