01_03 JSP基础语法之include

包含指令include

5.1静态包含

先包含,然后再编译处理。只是简单把代码全部整合在一起。

语法:<%@include file="要包含的文件路径"%>

例:

<h2>
<font color="red">
	info.htm
</font>
</h2>
<h2>
<font color="blue">
	info.inc
</font>
</h2>
<h2>
<font color="green">
<%="info.jsp"%>
</font>
</h2>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
	<title>include</title>
</head>
<body>
	<h1>静态包含操作</h1>
	<%@include file="info.htm"%>
	<%@include file="info.jsp"%>
	<%@include file="info.inc"%>
</body
</html>



5.2动态包含

语法1:不传递参数

<jsp:include page={要包含的文件路径|<%=表达式%>}flush=true|false />

语法2:传递参数

<jsp:include page={要包含的文件路径|<%=表达式%>}flush=true|false

    <jsp:param name=参数名称 value=参数内容/>

    多个参数

</jsp:include>

例:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
	<title>include</title>
</head>
<body>
<%
	String username = "zhengzengguo";
%>
	<h1>动态包含并传递参数</h1>
	<jsp:include page="receive_param.jsp">
		<jsp:param name="name" value="<%=username%>" />
		<jsp:param name="info" value="www.126.com" />
	</jsp:include>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<h1>参数一:<%=request.getParameter("name")%></h1>
<h1>参数二:<%=request.getParameter("info")%></h1>


注意:

若出现文件定义变重名时,静态包含会发生错误,因为静态包含只是先整合再编译,会提示重命名。而动态包含则可以正常执行,因为动态包含是先编译再合并,故能正常执行。所以动态包含更加方便,应常使用动态包含。



以上内容参考JAVAWEB开发实战经典(名师讲坛)


你可能感兴趣的:(include)