1. 静态导入示例

先总结:

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

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

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

静态导入指令 :

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

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

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

error_jsp.class

error_jsp.java

index_jsp.class

index_jsp.java

index_jsp.java源码

  1. package org.apache.jsp;
  2.  
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import javax.servlet.jsp.*;
  6.  
  7. public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
  8. implements org.apache.jasper.runtime.JspSourceDependent {
  9.  
  10. private static final javax.servlet.jsp.JspFactory _jspxFactory =
  11. javax.servlet.jsp.JspFactory.getDefaultFactory();
  12.  
  13. private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
  14.  
  15. static {
  16. _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1);
  17. _jspx_dependants.put("/error.jsp", Long.valueOf(1429692772031L));
  18. }
  19.  
  20. private javax.el.ExpressionFactory _el_expressionfactory;
  21. private org.apache.tomcat.InstanceManager _jsp_instancemanager;
  22.  
  23. public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
  24. return _jspx_dependants;
  25. }
  26.  
  27. public void _jspInit() {
  28. _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
  29. _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  30. }
  31.  
  32. public void _jspDestroy() {
  33. }
  34.  
  35. public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
  36. throws java.io.IOException, javax.servlet.ServletException {
  37.  
  38. final javax.servlet.jsp.PageContext pageContext;
  39. javax.servlet.http.HttpSession session = null;
  40. final javax.servlet.ServletContext application;
  41. final javax.servlet.ServletConfig config;
  42. javax.servlet.jsp.JspWriter out = null;
  43. final java.lang.Object page = this;
  44. javax.servlet.jsp.JspWriter _jspx_out = null;
  45. javax.servlet.jsp.PageContext _jspx_page_context = null;
  46.  
  47. try {
  48. response.setContentType("text/html; charset=UTF-8");
  49. pageContext = _jspxFactory.getPageContext(this, request, response,
  50. null, true, 8192, true);
  51. _jspx_page_context = pageContext;
  52. application = pageContext.getServletContext();
  53. config = pageContext.getServletConfig();
  54. session = pageContext.getSession();
  55. out = pageContext.getOut();
  56. _jspx_out = out;
  57.  
  58. out.write("\r\n");
  59. out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
  60. out.write("<html>\r\n");
  61. out.write("<head>\r\n");
  62. out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
  63. out.write("<title>静态导入</title>\r\n");
  64. out.write("</head>\r\n");
  65. out.write("<body>\r\n");
  66. out.write(" ");
  67. out.write("\r\n");
  68. out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
  69. out.write("<html>\r\n");
  70. out.write("<head>\r\n");
  71. out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
  72. out.write("<title>这是一个静态页面</title>\r\n");
  73. out.write("</head>\r\n");
  74. out.write("<body>\r\n");
  75. out.write(" 静态页面内容\r\n");
  76. out.write("</body>\r\n");
  77. out.write("</html>");
  78. out.write("\r\n");
  79. out.write("</body>\r\n");
  80. out.write("</html>");
  81. } catch (java.lang.Throwable t) {
  82. if (!(t instanceof javax.servlet.jsp.SkipPageException)){
  83. out = _jspx_out;
  84. if (out != null && out.getBufferSize() != 0)
  85. try { out.clearBuffer(); } catch (java.io.IOException e) {}
  86. if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
  87. else throw new ServletException(t);
  88. }
  89. } finally {
  90. _jspxFactory.releasePageContext(_jspx_page_context);
  91. }
  92. }
  93. }

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

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

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

2. 动态导入示例

动态导入指令

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

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

此时index_jsp.java的源码

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

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

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

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

3. 动态include传参数

静态include是不能传参数的

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>动态导入</title>
  7. </head>
  8. <body>
  9. <jsp:include page="error.jsp" flush="true">
  10. <jsp:param value="hello" name="param1"/>
  11. <jsp:param value="world" name="param2"/>
  12. </jsp:include>
  13. </body>
  14. </html>
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>这是一个静态页面</title>
  7. </head>
  8. <body>
  9. <%
  10. String param1 = request.getParameter("param1");
  11. String param2 = request.getParameter("param2");
  12. String param3 = "XXXXXOOOOOOO";
  13. %>
  14. <%=param1 %>&nbsp;&nbsp;<%=param2 %>&nbsp;&nbsp;<%=param3 %>
  15. </body>
  16. </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. PHP中常用的字符串函数?

    1.strlen() 2.strpos() 3.mb_strlen()

  2. yii2 刷新缓存(刷新模型缓存)

    Yii2开启表结构缓存,因为当运用模型(model)时,AR的一些公共属性都会从DB中获取,这样会导致服务器负担一些额外的资源开销,实际上对于成品来说,服务器这些开始销是多余的,故应该阻止这种默认行为 ...

  3. Linux文件类型及目录配置

    Linux文件类型与扩展名 在Linux系统中,任何硬件设备或者其他设备都是以文件的形式存在,就连数据通信的接口这些也是由专门的文件来负责的,因此Linux的文件种类就非常多,出了之前我们常见的 - ...

  4. System、应用程序进程的Binder线程池和Handler消息循环

    首先看一张Android系统启动流程图:

  5. 使用bbed编辑研究oracle数据块结构

    bbed是随oracle软件公布的一款数据块查看和编辑工具,作为一款内部工具.bbed的功能很强大,可是假设使用不当可能给数据库造成无法挽回的损失.因此.我们建议在使用bbed改动数据块前备份被改动的 ...

  6. UTI 唯一类型标识

    本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6657231   applicationdocumentationtypessys ...

  7. git更新代码出现错误

    git  pull代码时,出现如下的错误: SSL certificate problem: unable to get local issuer certificate 主要的原因是:没有ssl证书 ...

  8. node.js的安装与第一个hello world、node.js的初始化

    1.下载node.js文件 2.windows下点击安装  重复下一步即可 3.编辑工具  EditPlus编辑器 4.新建保存目录的文件夹,并新建一个文本文档 5.打开EditPlus编辑器  打开 ...

  9. 《从零开始学Swift》学习笔记(Day4)——用Playground工具编写Swift

    Swift 2.0学习笔记(Day4)——用Playground工具编写Swift 原创文章,欢迎转载.转载请注明:关东升的博客 用Playground编写Swift代码目的是为了学习.测试算法.验证 ...

  10. JavaScript深入理解sort()方法

    一. 基本用法 let arr1 = [3, 5, 7, 1, 8, 7, 10, 20, 19] console.log(arr1.sort()) // [1, 10, 19, 20, 3, 5, ...