包含指令include

5.1静态包含

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

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

例:

<h3><fontcolor="red">info.htm</font></h3>

<h3><fontcolor="blue">info.inc</font></h3>

<h3><fontcolor="green"><%="info.jsp"%></font></h3>

<%@pagecontentType="text/html"pageEncoding="GBK"%><html><head><title>include</title></head><body><h2>静态包含操作</h2><%@includefile="info.htm"%><%@includefile="info.jsp"%><%@includefile="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>

例:

<%@pagecontentType="text/html"pageEncoding="GBK"%><html><head><title>include</title></head><body><%Stringusername="zhengzengguo";%><h2>动态包含并传递参数</h2><jsp:includepage="receive_param.jsp"><jsp:paramname="name"value="<%=username%>"/><jsp:paramname="info"value="www.126.com"/></jsp:include></body></html>

<%@pagecontentType="text/html"pageEncoding="GBK"%><h2>参数一:<%=request.getParameter("name")%></h2><h2>参数二:<%=request.getParameter("info")%></h2>


注意:

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



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