包含指令include
5.1静态包含
先包含,然后再编译处理。只是简单把代码全部整合在一起。
语法:<%@include file="要包含的文件路径"%>
例:
<h3>
<font color="red">
info.htm
</font>
</h3>
<h3>
<font color="blue">
info.inc
</font>
</h3>
<h3>
<font color="green">
<%="info.jsp"%>
</font>
</h3>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>include</title>
</head>
<body>
<h2>静态包含操作</h2>
<%@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";
%>
<h2>动态包含并传递参数</h2>
<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"%>
<h2>参数一:<%=request.getParameter("name")%></h2>
<h2>参数二:<%=request.getParameter("info")%></h2>
注意:
若出现文件定义变重名时,静态包含会发生错误,因为静态包含只是先整合再编译,会提示重命名。而动态包含则可以正常执行,因为动态包含是先编译再合并,故能正常执行。所以动态包含更加方便,应常使用动态包含。
以上内容参考JAVAWEB开发实战经典(名师讲坛)
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。