1.include静态包含 格式: <%@include file="要包含的页面地址"%> 特点: 1. 将内容进行了直接的替换 2. 静态包含只会生成一个源码文件,最终的内容全部在_jspService方法体中(源码文件中) 3. 不能出现同名变量 4. 运行效率高一点点。耦合性较高,不够灵活。
2.include动态包含 格式: <jsp:include page="要包含的页面路径"></jsp:include> 特点: 1. 动态包含相当于方法的调用 2. 动态包含会生成多个源码文件 3. 可以定义同名变量 4. 效率高,耦合度低 注: 当动态包含不需要传递参数时,include双标签之间不要有任何内容,包括换行和空格 使用动态包含传递参数 <jsp:include page="要包含的页面路径"> <jsp:param name="参数名" value="参数值"/> </jsp:include> 注:name属性不支持表达式,value属性支持表示 获取参数: request.getParameter(name); 通过指定参数名获取参数值
04-footer.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>底部</title>
</head>
<body>
<h2>底部内容</h2>
<%
int a = 10;
// 获取动态包含传递的参数
String uname = request.getParameter("uname");
String msg = request.getParameter("msg");
out.print(uname+","+msg);
%>
</body>
</html>
04-header.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>头部</title>
</head>
<body>
<h2>头部内容</h2>
<%
int num = 1;
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>include静态包含</title>
</head>
<body>
<%--
include静态包含
格式:
<%@include file="要包含的页面地址"%>
特点:
1. 将内容进行了直接的替换
2. 静态包含只会生成一个源码文件,最终的内容全部在_jspService方法体中(源码文件中)
3. 不能出现同名变量
4. 运行效率高一点点。耦合性较高,不够灵活。
--%>
<%@include file="04-header.jsp"%>
<h2>主体内容</h2>
<%
// int num = 1;
%>
<%@include file="04-footer.jsp"%>
</body>
</html>
05-include动态包含.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>include动态包含</title>
</head>
<body>
<%--
include动态包含
格式:
<jsp:include page="要包含的页面路径"></jsp:include>
特点:
1. 动态包含相当于方法的调用
2. 动态包含会生成多个源码文件
3. 可以定义同名变量
4. 效率高,耦合度低
注:
当动态包含不需要传递参数时,include双标签之间不要有任何内容,包括换行和空格
使用动态包含传递参数
<jsp:include page="要包含的页面路径">
<jsp:param name="参数名" value="参数值"/>
</jsp:include>
注:name属性不支持表达式,value属性支持表示
获取参数:
request.getParameter(name); 通过指定参数名获取参数值
--%>
<jsp:include page="04-header.jsp"></jsp:include>
<h2>主体内容</h2>
<%
int a = 1;
%>
<jsp:include page="04-footer.jsp"></jsp:include>
<%-- 动态包含传递参数 --%>
<%
String str = "hello";
String url = "04-footer.jsp";
%>
<jsp:include page="<%=url%>">
<jsp:param name="uname" value="admin"/>
<jsp:param name="msg" value="<%=str%>"/>
</jsp:include>
</body>
</html>
3. 当动态包含不需要传递参数时,include双标签之间不要有任何内容,包括换行和空格
org.apache.jasper.JasperException: /05-include动态包含.jsp (行.: [38], 列: [39]) 使用“name”和“value”属性期望“jsp:param”标准操作