1. 指令include

<%@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" %>

2. 动作include

通过动作

<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" />

3. 指令include和动作include的区别

通过之前的学习知道,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,然后把返回的结果,嵌入到响应中。

4. 传参

因为指令<%@include 会导致两个jsp合并成为同一个java文件,所以就不存在传参的问题,在发出hello.jsp 里定义的变量,直接可以在footer.jsp中访问。

而动作其实是对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

<hr>
    <p style="text-align:center">copyright@<%=request.getParameter("year")%>
</p>

有帮到你的点赞、收藏一下吧

                                                                        需要更多教程,微信扫码即可

                                                                                 

                                                                                         👆👆👆

                                                        别忘了扫码领资料哦【高清Java学习路线图】

                                                                     和【全套学习视频及配套资料】
 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐