jsp页面包含的几中方式
(1)include指令
include指令告诉容器:复制被包含文件汇总的所有内容,再把它粘贴到这个文件中。
<%@ include file="Header.jsp"%>
(2)include标准动作
<jsp:include page="Header.jsp"/>
(3)采用JSTL
<c:import url="http://www.sina.com/index.html">
<jsp:include>和<%@include%>的区别
<%@include%>和<jsp:include>的区别,发现了一些东西的。
<%@include%>:页面请求之前预编译,所有代码包含进来之后,一起进行处理,把所有代码合在一起,编译成一个servlet
<jsp:include>:所有代码分别处理,在页面被请求的时候才编译,被编译成多个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> :动态包含
第一种情况(<jsp:include>包含的是html文件):
DynamicInclude.jsp:
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>动态包含</title>
</head>
<bodystyle=" margin: 0px; padding: 0px; color: rgb(0, 0, 255);">> <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 :
<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");
<span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.html", out, true);</span>
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=" margin: 0px; padding: 0px; color: rgb(0, 0, 255);">> <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");
<span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.jsp", out, true);</span>
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>");
<%@include%>:静态包含
第一种情况:<%@include%>包含的是jsp文件。
StaticInclude.jsp:
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>静态包含</title>
</head>
<bodystyle=" margin: 0px; padding: 0px; color: rgb(0, 0, 255);">> <%@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");
<span style="color:#ff0000;">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>");</span>
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=" margin: 0px; padding: 0px; color: rgb(0, 0, 255);">> <%@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");
<span style="color:#ff0000;">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>");</span>
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页面包含的几中方式的更多相关文章
- JSP页面包含其他页面的三种方式及区别
一. <%@ include file="header.inc"%> 该指令在编译之前先读入指定的页面中的内容(并不对动态内容求值),融合后的完整页面再被整体的转换为一 ...
- 关于JAVA EE项目在WEB-INF目录下的jsp页面如何访问WebRoot中的CSS和JS文件
找了这么久资料,总算解决了 感谢博客园:http://www.cnblogs.com/xsht/p/5275081.html 感谢百度:http://zhidao.baidu.com/link?url ...
- jsp页面和js代码中使用sessionScope获取session值
场景:有些实体对象可以放到HttpSession对象中,保正在一个会话期间可以随时获取这个对象的属性,例如可以将登录用户的信息写入session,以保证页面随时可以获取并显示这个用户的状态信息.下面以 ...
- 在jsp页面动态添加数据库中的内容
工具:myeclipse+oracle11g 1.首先新建一个jsp页面. 2.在src目录下新建实体类User.java 3.创建BaseDao用来链接数据库 4.在pl/sql中创建存储过程 5. ...
- html或者jsp页面引用jar包中的js文件
一,页面上引用jar包中的js文件的方法 使用java web框架AppFuse的时候发现,jquery.bootstrap等js框架都封装到jar包里面了.这些js文件通过一个wro4j的工具对其进 ...
- jsp页面取得一对多中的set集合的size
jsp中使用${list.size }会编译成list.getSize()方法,并不能获取list的长度,因为程序回去找List对象中的getSize()方法,所以只能想别的办法, 一种方法是在后台程 ...
- JSP页面输出的几种方式:
1. 内置九大对象之out 下载图片 2. <%= %> JSP输出表达式 JSP中出现大量脚本 3. response.getWriter() n ...
- jsp页面 直接从地址栏中 获取 参数的方法
function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=([^&am ...
- jsp页面从标签属性中获取值
你还可以在"data-*" 属性里使用json语法,例如 <div id="awesome-json" data-awesome='{"game ...
随机推荐
- EmbarassedBirds全体开发人员落泪
Github (李昆乘,赖展飞) 现阶段还在开发后期,API调试过程中. 本周无法上线. 全体开发人员留下眼泪. 贴上几个功能图, 给大家尝尝鲜吧! 现阶段仍在API调试 因为队员李昆乘经常出去玩没有 ...
- vim 编辑器IDE版
wget https://raw.github.com/ma6174/vim/master/setup.sh -O ma6174_vim_setup.sh && bash ma6174 ...
- 集训Day4
在bzoj刷了好几天杂题感觉手感不是很好 继续回来集训一下 好几天没更新了啊... bzoj1875 一个无向图,一个人要从起始点走$t$步走到终点,不能沿着刚走过来那条边回去,问有多少种走法 $m ...
- Spring笔记03(Spring创建对象的三种方式)
1.创建对象的三种方式和bean的生命周期的验证: Animal接口代码: package cn.pb.dao; /** * 动物接口 */ public interface Animal { //吃 ...
- 1067 Bash 游戏v2
传送门 1067 Bash游戏 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次只能拿1,3,4颗,拿到最后1颗石子的人 ...
- unicode和utf-8互转
1.1 ASCII码 我们知道, 在计算机内部, 所有的信息最终都表示为一个二进制的字符串. 每一个二进制位(bit)有0和1两种状态, 因此八个二进制位就可以组合出 256种状态, 这被称为一个字节 ...
- Jenkins持续集成环境搭建
1部署Jenkins Jenkins部署很简单,只需建立一个新应用,将Jenkins的war包部署进去,再分配好权限就可以了. 1.1创建应用 建立一个新Nginx应用:jenkins.com 1.2 ...
- [原创]如何解决IE10下CkEditor报 --- SCRIPT5007: 无法获取未定义或 null 引用的属性“toLowerCase”
如何解决IE10下CkEditor报 --- SCRIPT5007: 无法获取未定义或 null 引用的属性“toLowerCase” 错误 如果你的IE是IE10,且不是运行在IE的兼容模式你也许会 ...
- POJ2887(块状链表)
Big String Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 6346 Accepted: 1525 Descr ...
- css3 实现运动动画 圆与椭圆
圆: html <div class="demo4"><div></div></div> css .demo4{ width: 20 ...