<%@include%>和<jsp:include>的区别.本人在研究这里的内容的时候碰到了一些疑惑的地方,故找了一篇文章参考了一下!
文章中有一些出入,所以自己又修改了一番!
<%@include%>:我们知道JSP文件本身就是servlet,然而JSP文件需要预先编译成servlet,也就是class文件,而<%@include%>页面jsp在请求之前就已经进行了预编译,所有代码包含进来之后,一起进行处理,把所有代码合在一起,编译成一个servlet!
<jsp:include>:所有JSP文件代码分别处理,是在页面被请求的时候才进行编译,将多个jsp文件编译成多个servlet,页面语法相对独立,处理完成之后再将代码的显示结果(处理结果)组合进来。
显而易见,两者的区别就区别开了!
随后我们来看一下JSP中的两种包含页面的用法!
第一种:include指令:当JSP转换成Servlet时引入指定文件
<%@ pagecontentType="text/html;charset=GB2312" language="java"errorPage=""%>
<%@ include file="head.jsp"%>
<%@ include file="body.jsp"%>
<%@ include file="tail.jsp"%>
第二种:<jsp:include>动作元素:当JSP页面被请求时引入指定文件
<%@ page contentType="text/html; charset=GB2312"language="java" errorPage=""%>
<jsp:include page="head.jsp"/>
<jsp:include page="body.jsp"/>
<jsp:include page="tail.jsp"/>
第二种方法可以很方便的用<jsp:param>来向所包含页传递参数,方法如下:
<%@ page contentType="text/html; charset=GB2312"language="java" errorPage=""%>
<jsp:include page="head.jsp"/>
<jsp:includepage="body.jsp">
<jsp:param name="uid"value="username"/>
<jsp:param name="pwd"value="password"/>
</jsp:include>
<jsp:includepage="tail.jsp"/>
<jsp:include> :动态包含html文件和JSP文件的区别
第一种情况(<jsp:include>包含的是html文件):
DynamicInclude.jsp:
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>动态包含</title>
</head>
<body style="background-color:lightblue">
<jsp:include page="header.html"flush="true"/><!--动态包含-->
<tableborder="1" align="center">
<tr>
<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>
</tr>
<tr>
<td>a</td><td>b</td><td>c</td><td>d</td>
</tr>
</table>
</body>
</html>
Header.html :
(注意,JSP本身就是servlet,而html却不是,也就是此处最大的却别了!)
<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
动态包含的标题(HTML)
</h2>
运行之后,只生成一个servlet,和上面的代码对应如下:
out.write("\r\n");
out.write("<html>\r\n");
out.write("\t<head>\r\n");
out.write("\t\t<title>动态包含</title>\r\n");
out.write("\t</head>\r\n");
out.write("\t<bodystyle=\"background-color:lightblue\">\r\n");
out.write("\r\n");
out.write("\t\t");
org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.html", out, true);
out.write("<!--动态包含-->\r\n");
out.write("\r\n");
out.write("\t\t<table border=\"1\"align=\"center\">\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t</table>\r\n");
out.write("\t</body>\r\n");
out.write("</html>");
第二种情况(<jsp:include>包含的是jsp文件):
DynamicInclude.jsp:
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>动态包含</title>
</head>
<bodystyle="background-color:lightblue">
<jsp:include page="header.jsp"flush="true"/><!--动态包含-->
<tableborder="1" align="center">
<tr>
<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>
</tr>
<tr>
<td>a</td><td>b</td><td>c</td><td>d</td>
</tr>
</table>
</body>
</html>
Header.jsp :
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<body>
<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
动态包含的标题(JSP)
</h2>
</body>
</html>
运行之后,生成了两个servlet,DynamicInclude_jsp.java和header_jsp.java
这也是为什么 Header.jsp中要写上<%@page contentType="text/html;charset=gb2312"%>和完整的<html></html>和<body></body>
而Header.html不用写的原因。因为后者两个.jsp文件是两个相互独立的整体,它们之间的关系是通过request和reponse来发生的,而前者只是简单的嵌套。后者的两个servlet对应的代码如下:
DynamicInclude_jsp.java:
out.write("\r\n");
out.write("<html>\r\n");
out.write("\t<head>\r\n");
out.write("\t\t<title>动态包含</title>\r\n");
out.write("\t</head>\r\n");
out.write("\t<bodystyle=\"background-color:lightblue\">\r\n");
out.write("\r\n");
out.write("\t\t");
org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.jsp", out, true);
out.write("<!--动态包含-->\r\n");
out.write("\r\n");
out.write("\t\t<table border=\"1\"align=\"center\">\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t</table>\r\n");
out.write("\t</body>\r\n");
out.write("</html>");
header_jsp.java:
out.write("\r\n");
out.write("<html>\r\n");
out.write("\t<body>\r\n");
out.write("\t\t<h2 style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");
out.write("\t\t\t动态包含的标题(JSP)\r\n");
out.write("\t\t</h2>\r\n");
out.write("\t</body>\r\n");
out.write("</html>");
从而我们可以清楚的明白了,<jsp:include file=""> 引入html和jsp的区别了!
<%@include%>:静态包含
第一种情况:<%@include%>包含的是jsp文件。
StaticInclude.jsp:
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>静态包含</title>
</head>
<bodystyle="background-color:lightblue">
<%@include file="header.jsp"%><!--静态包含-->
<tableborder="1" align="center">
<tr>
<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>
</tr>
<tr>
<td>a</td><td>b</td><td>c</td><td>d</td>
</tr>
</table>
</body>
</html>
header.jsp:
<%@pagecontentType="text/html;charset=gb2312"%>
<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
静态包含的标题(JSP)
</h2>
运行之后,只生成一个servlet,和上面的代码对应如下:
out.write("\r\n");
out.write("<html>\r\n");
out.write("\t<head>\r\n");
out.write("\t\t<title>静态包含</title>\r\n");
out.write("\t</head>\r\n");
out.write("\t<body style=\"background-color:lightblue\">\r\n");
out.write("\r\n");
out.write("\t\t");
out.write("\r\n");
out.write("<h2style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");
out.write("\t静态包含的标题(JSP)\r\n");
out.write("</h2>");
out.write("<!--静态包含-->\r\n");
out.write("\t\t<table border=\"1\"align=\"center\">\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t</table>\r\n");
out.write("\t</body>\r\n");
out.write("</html>");
第二种情况:当<%@include%>包含的是html文件。
StaticInclude.jsp:
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>静态包含</title>
</head>
<bodystyle="background-color:lightblue">
<%@include file="header.html"%><!--静态包含-->
<tableborder="1" align="center">
<tr>
<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>
</tr>
<tr>
<td>a</td><td>b</td><td>c</td><td>d</td>
</tr>
</table>
</body>
</html>
header.html:
<%@pagecontentType="text/html;charset=gb2312"%>
<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
静态包含的标题(HTML)
</h2>
运行之后,也是只生成一个servlet,和上面的代码对应如下:
out.write("\r\n");
out.write("<html>\r\n");
out.write("\t<head>\r\n");
out.write("\t\t<title>静态包含</title>\r\n");
out.write("\t</head>\r\n");
out.write("\t<bodystyle=\"background-color:lightblue\">\r\n");
out.write("\r\n");
out.write("\t\t");
out.write("\r\n");
out.write("<h2style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");
out.write("\t静态包含的标题(HTML)\r\n");
out.write("</h2>");
out.write("<!--静态包含-->\r\n");
out.write("\t\t<table border=\"1\"align=\"center\">\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t\t<tr>\r\n");
out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");
out.write("\t\t\t</tr>\r\n");
out.write("\t\t</table>\r\n");
out.write("\t</body>\r\n");
out.write("</html>");
由上可以总结出:
对于静态包含,<%@include%>,中包含的文件,只是简单的嵌入到主文件中,就是在jsp页面转化成Servlet时才嵌入到主文件中,因为运行的结果是只生成了一个Servlet。
而对于动态包含<jsp:incude>,如果被包含文件是动态的,那么就会生成两个Servlet,也就是被包含文件也要经过jsp引擎编译执行生成一个Servlet,两个Servlet通过request和reponse进行通信。如果被包含的文件是静态的,那么这种情况和<%@include>就很相似,只生成了一个Servlet,但是他们之间没有进行简单的嵌入,而依然是通过request和reponse进行的通信。
分享到:
相关推荐
要掌握JSP指令(page, include, taglib),隐式对象(如request, response, session, application等),以及JSP表达式和脚本元素的使用。 3. **EL(Expression Language)**:EL是一种简洁的查询语言,用于在JSP...
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
wrf转mp4播放器1.1.1
内容概要:本文档详细介绍了如何在Simulink中设计一个满足特定规格的音频带ADC(模数转换器)。首先选择了三阶单环多位量化Σ-Δ调制器作为设计方案,因为这种结构能在音频带宽内提供高噪声整形效果,并且多位量化可以降低量化噪声。接着,文档展示了具体的Simulink建模步骤,包括创建模型、添加各个组件如积分器、量化器、DAC反馈以及连接它们。此外,还进行了参数设计与计算,特别是过采样率和信噪比的估算,并引入了动态元件匹配技术来减少DAC的非线性误差。性能验证部分则通过理想和非理想的仿真实验评估了系统的稳定性和各项指标,最终证明所设计的ADC能够达到预期的技术标准。 适用人群:电子工程专业学生、从事数据转换器研究或开发的技术人员。 使用场景及目标:适用于希望深入了解Σ-Δ调制器的工作原理及其在音频带ADC应用中的具体实现方法的人群。目标是掌握如何利用MATLAB/Simulink工具进行复杂电路的设计与仿真。 其他说明:文中提供了详细的Matlab代码片段用于指导读者完成整个设计流程,同时附带了一些辅助函数帮助分析仿真结果。
国网台区终端最新规范
《基于YOLOv8的智慧农业水肥一体化控制系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计
GSDML-V2.33-LEUZE-AMS3048i-20170622.xml
微信小程序项目课程设计,包含LW+ppt
微信小程序项目课程设计,包含LW+ppt
终端运行进度条脚本
幼儿园预防肺结核教育培训课件资料
python,python相关资源
《基于YOLOv8的智慧校园电动车充电桩状态监测系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计
deepseek 临床之理性软肋.pdf
SM2258XT量产工具(包含16种程序),固态硬盘量产工具使用
RecyclerView.zip
水务大脑让水务运营更智能(23页)
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
大众捷达轿车前轮制动器设计
《基于YOLOv8的智能工厂压缩空气泄漏检测系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计