包含操作
使用包含操作,可以将一些重复的代码包含进来继续使用
静态包含
静态包含语法:<%@ include file="要包含的文件路径"%>
<span style="font-size:18px;">/*main.jsp*/
<%@ page language="java" contentType="text/html; charset=utf8" pageEncoding="utf8"%>
<%@ include file="head.jsp" %>
<h2 style="color:blue">main</h2>
<%@ include file="foot.jsp" %>
/*head.jsp*/
<%@ page language="java" contentType="text/html; charset=utf8" pageEncoding="utf8" %>
<h2 style="color:red">head</h2>
/*foot.jsp*/
<%@ page language="java" contentType="text/html; charset=utf8" pageEncoding="utf8" %>
<h2 style="color:green">foot</h2></span>
动态包含
使用<jsp:include>指令可以完成动态包含的操作,与之前的静态包含不同,动态包含语句,可以自动区分被包含的页面是静态还是动态。
传递参数:<jsp:include page="{要包含的文件路径| <%=表达式%>}" flush="true |false">
两种包含的区别:
<jsp:param name="参数名称" value="参数内容"/>
...可以向被包含页面中传递多个参数
</jsp:include>
<span style="font-size:18px;">/*host.jsp*/
<%@ page language="java" contentType="text/html; charset=utf8" pageEncoding="utf8" %>
<h2>host.jsp</h2>
<jsp:include page="client.jsp">
<jsp:param name="name" value="zhang"/>
<jsp:param name="password" value="123456" />
</jsp:include>
/*client.jsp*/
<%@ page language="java" contentType="text/html; charset=utf8" pageEncoding="utf8"%>
<h2>client.jsp</h2>
name=<%= request.getParameter("name")%>
password=<%=request.getParameter("password")%>
</span>
两种包含的区别:
静态包含:先包含,再处理,不可以传递参数
动态包含:先处理,再包含,可以传递参数