1. 静态导入示例

先总结:

1:静态include是把被引入的文件拼接到本页面中,再做为一个整体来编译,返回结果给客户端。

动态include是分别编译本页面和被引入的页面,再把结果合成一个html,返回结果给客户端。

2:动态include可以传参数,可以include静态页面,也可以Include动态页面

静态导入指令 :

<%@include file="error.jsp"%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>静态导入</title>
</head>
<body>
<%@include file="error.jsp"%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>这是一个静态页面</title>
</head>
<body>
静态页面内容
</body>
</html>

查看jsp编译之后的文件可以看到,index.jsp和error.jsp分别被编译成java文件和对应的class文件

error_jsp.class

error_jsp.java

index_jsp.class

index_jsp.java

index_jsp.java源码

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*; public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent { private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; static {
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1);
_jspx_dependants.put("/error.jsp", Long.valueOf(1429692772031L));
} private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
} public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
} public void _jspDestroy() {
} public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException { final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null; try {
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out; out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("<title>静态导入</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write(" ");
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("<title>这是一个静态页面</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write(" 静态页面内容\r\n");
out.write("</body>\r\n");
out.write("</html>");
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}

从源代码里面可以看到,error.jsp的内容被整个融入到了index.jsp中

结论:静态include是将被导入页面的代码完全融入本页面中,两个页面融合成一个整体Servlet,返回给客户端

有一个问题,当被引入的页面中和本页面中有相同的变量定义时,会报错

2. 动态导入示例

动态导入指令

<jsp:include page="error.jsp" flush="true"></jsp:include>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态导入</title>
</head>
<body>
<jsp:include page="error.jsp" flush="true"></jsp:include>
</body>
</html>

此时index_jsp.java的源码

out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("<title>动态导入</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write(" ");
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "error.jsp", out, true);
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");

可以看到动态include只是用了include方法引入了被导入的文件

结论: 动态include先编译被引入的文件,在包含到本页面中

被引入的文件和本页面中有相同的变量定义时也不会报错

3. 动态include传参数

静态include是不能传参数的

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态导入</title>
</head>
<body>
<jsp:include page="error.jsp" flush="true">
<jsp:param value="hello" name="param1"/>
<jsp:param value="world" name="param2"/>
</jsp:include>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>这是一个静态页面</title>
</head>
<body>
<%
String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");
String param3 = "XXXXXOOOOOOO";
%>
<%=param1 %>&nbsp;&nbsp;<%=param2 %>&nbsp;&nbsp;<%=param3 %>
</body>
</html>

结果: hello  world  XXXXXOOOOOOO

JSP的动态Include的静态Include的更多相关文章

  1. jsp的动态包含和静态包含

    jsp的动态包含和静态包含 例如:提取一个公共的页面(top.jsp)到/WEB-INF/jsp/common/目录下 动态包含: 被包含的页面也会独立编译,生成字节码文件,一般包含页面信息频繁变化的 ...

  2. JSP中动态INCLUDE与静态INCLUDE的区别

    动态INCLUDE 用法: <jsp:include page="included.jsp" flush="true" /> 说明: 它总是会检查所 ...

  3. JSP中动态include和静态include的区别(简版)

    动态的include: 用法:<jsp:include page="1.jsp" flush="true" /> 特点:行为元素,可以带参数:先编译 ...

  4. jsp动态include和静态Include

    动态 INCLUDE 用 jsp:include 动作实现 <jsp:include page="included.jsp" flush="true" / ...

  5. JSP 中动态 INCLUDE 与静态 INCLUDE 的区别?

    一.静态包含指令<%@include file="fileurl"%> 两个jsp页面的<%@page contentType="text/html:c ...

  6. JSP中动态include和静态include区别

    静态include(<%@ include file=""%>): 静态include(静态导入)是指将一个其他文件(一个jsp/html)嵌入到本页面 jsp的inc ...

  7. JSP中动态INCLUDE与静态INCLUDE的区别?

    动态INCLUDE用jsp:include动作实现 它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数 静态INCLUDE用include伪码实现,定不会检查所含文件的变化,适用于包含 ...

  8. JSP中动态include与静态include的区别介绍

    转自:https://m.jb51.net/article/43304.htm 动态INCLUDE 用法:<jsp:include page="included.jsp" f ...

  9. 动态include与静态include的区别

    搬一下以前写的 个人总结: 动态INCLUDE 用jsp:include动作实现 <jsp:include page="included.jsp" flush="t ...

随机推荐

  1. Yii GridView::widget

    GridView::widget文档 http://demos.krajee.com/grid GridView::widget([ 'dataProvider' => $dataProvide ...

  2. 集成 SOLR 到 TOMCAT 中(傻瓜教程)

    按照如下配置,整个 Solr 是绿色版的,可以将 Tomcat 目录复制到任何一个地方运行 1.下载 solr 4.3 版本 2.下载 Tomcat 7 ( 6 也可以),另外可以根据系统下载 32 ...

  3. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.4——Flavor Dimensions

    问题: 一个product flavor不够,你需要另一个标准去区分不同版本的app 解决方案: 在product flavor中增加flavorDimensions 讨论: 在3.2章展示了一个有三 ...

  4. Docker学习(2Docker基本命令 )

    1.首先我们需要明确在docker中需要了解的一些基础知识 Docker虚拟化有三个概念需要理解,分别镜像.容器.仓库. 1) 镜像:docker的镜像其实就是模板,跟我们常见的ISO镜像类似,是一个 ...

  5. SVG 与 Canvas:如何选择

    SVG 与 Canvas:如何选择 61(共 69)对本文的评价是有帮助 - 评价此主题   本主题一开始将对 SVG 与 Canvas 进行简要比较,接下来会讨论大量的比较代码示例,如光线跟踪和绿屏 ...

  6. hihocoder 1279(状压)

    坑爹的题目.不过不能说不是一道挺好的题目. 坑主要坑在,妹的我一样的复杂度,写的姿势略差了点然后就一直超时. 比赛的时候我还直接就看错题目,把AND运算看成了OR...还敲完交了一发. 这题很容易想到 ...

  7. Docker selinux

    编辑/etc/sysconfig/docker文件,把OPTIONS='--selinux-enabled'的--selinux-enabled注释掉,也可以通过这个错误. 最大的问题就是Linux的 ...

  8. SqlProfiler的替代品-ExpressProfiler

    可以用来跟踪执行的sql语句.安装SqlServer之后SqlServerManagementStudio自带一个SqlProfiler,但是如果安装的SqlExpress,那就没有了. 项目的主页在 ...

  9. Android Studio的快捷键

    Android Studio可以在setting的keymaps设置快捷键,但最好使用该默认的快捷键. 生成TAG: logt 控制台打印带参的log:logm 代码提示:ctrl + alt + s ...

  10. Numpy用于数组的文件输入输出

    这一章比较简单,内容也比较少.而且对于文件的读写,还是使用pandas比较好.numpy主要是读写文本数据和二进制数据的. 将数组以二进制的格式保存到硬盘上 主要的函数有numpy.save和nump ...