步骤 2 : 指令include 步骤 3 : 动作include 步骤 4 : 指令include和动作include的区别 步骤 5 : 传参 <hr>
<p style="text-align:center">copyright@2016
</p>
<hr> <p style="text-align:center">copyright@2016 </p>
通过指令
<%@include file="footer.jsp" %> 在hello.jsp中包含该页面 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
你好 JSP
<%@include file="footer.jsp" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> 你好 JSP <%@include file="footer.jsp" %>
通过动作
<jsp:include page="footer.jsp" /> 在hello.jsp中包含该页面 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
你好 JSP
<jsp:include page="footer.jsp" />
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> 你好 JSP <jsp:include page="footer.jsp" />
通过之前的学习知道,JSP最后会被转译成Servlet
如果是指令include <%@include file="footer.jsp" %> footer.jsp的内容会被插入到 hello.jsp 转译 成的hello_jsp.java中,最后只会生成一个hello_jsp.java文件 如果是动作include <jsp:include page="footer.jsp" /> footer.jsp的内容不会被插入到 hello.jsp 转译 成的hello_jsp.java中,还会有一个footer_jsp.java独立存在。 hello_jsp.java 会在服务端访问footer_jsp.java,然后把返回的结果,嵌入到响应中。
因为指令<%@include 会导致两个jsp合并成为同一个java文件,所以就不存在传参的问题,在发出hello.jsp 里定义的变量,直接可以在footer.jsp中访问。
而动作<jsp:include />其实是对footer.jsp进行了一次独立的访问,那么就有传参的需要。 如本例: 1. 在hello.jsp中使用动作<jsp:include,并通过<jsp:param 带上参数 <jsp:include page="footer.jsp"> <jsp:param name="year" value="2017" /> </jsp:include> 2. 在footer.jsp中,使用request.getParameter("year")取出year
<%@page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
你好 JSP
<%=new Date().toLocaleString()%>
<jsp:include page="footer.jsp">
<jsp:param name="year" value="2017" />
</jsp:include>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> 你好 JSP <%=new Date().toLocaleString()%> <jsp:include page="footer.jsp"> <jsp:param name="year" value="2017" /> </jsp:include>
<hr>
<p style="text-align:center">copyright@<%=request.getParameter("year")%>
</p>
<hr> <p style="text-align:center">copyright@<%=request.getParameter("year")%> </p>
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
![]()
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|