龙空技术网

《JSP》第09节:JSP三大指令之include指令

Java新手笔记 39

前言:

而今兄弟们对“jsp中include”大概比较讲究,小伙伴们都想要剖析一些“jsp中include”的相关知识。那么小编在网摘上搜集了一些有关“jsp中include””的相关资讯,希望小伙伴们能喜欢,大家一起来了解一下吧!

JSP指令是指:用于设置JSP页面相关属性的一个语法命令,例如:设置页面编码字符集、导入其他包等等。JSP中提供了三个指令,分别是:page指令、include指令、taglib指令。其中page指令用于设置JSP页面属性,include指令用于引入其他的JSP文件,taglib指令用于引入标签库。这一小节内容介绍include指令的使用。

1.1、include指令作用

include指令作用:将指定的文件引入到当前JSP页面里面。include指令会将引入的文件内容嵌入到当前JSP页面中的对应位置。

<%@ include file="文件的相对路径" %>

案例代码:

创建一个header.jsp文件。

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>这是HTML头部</title></head>
创建一个include.jsp文件。
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%-- 引入头部文件 --%><%@ include file="header.jsp" %><body><div style="background-color: cadetblue">    这是正文内容区域</div><%-- 引入底部文件 --%><%@ include file="footer.html" %></body></html>
创建一个footer.jsp文件。
<%@ page contentType="text/html;charset=UTF-8" language="java" %><div style="background-color: antiquewhite;">  这是footer底部内容区域</div>

启动Tomcat容器,浏览器访问,结果如下:

1.2、include指令的本质

include指令的本质是什么呢???我们来查看下编译之后的Java源代码,找到上面include.jsp文件编译之后的源文件,如下所示:

/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/8.5.98 * Generated at: 2024-02-25 05:06:41 UTC * Note: The last modified time of this file was set to *       the last modified time of the source file after *       generation to assist with modification tracking. */package com.gitcode.servlet;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;public final class include_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent,                 org.apache.jasper.runtime.JspSourceImports {  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>(2);    _jspx_dependants.put("/footer.jsp", Long.valueOf(1708837593266L));    _jspx_dependants.put("/header.jsp", Long.valueOf(1708837593271L));  }  private static final java.util.Set<java.lang.String> _jspx_imports_packages;  private static final java.util.Set<java.lang.String> _jspx_imports_classes;  static {    _jspx_imports_packages = new java.util.HashSet<>();    _jspx_imports_packages.add("javax.servlet");    _jspx_imports_packages.add("javax.servlet.http");    _jspx_imports_packages.add("javax.servlet.jsp");    _jspx_imports_classes = null;  }  private volatile javax.el.ExpressionFactory _el_expressionfactory;  private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {    return _jspx_dependants;  }  public java.util.Set<java.lang.String> getPackageImports() {    return _jspx_imports_packages;  }  public java.util.Set<java.lang.String> getClassImports() {    return _jspx_imports_classes;  }  public javax.el.ExpressionFactory _jsp_getExpressionFactory() {    if (_el_expressionfactory == null) {      synchronized (this) {        if (_el_expressionfactory == null) {          _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();        }      }    }    return _el_expressionfactory;  }  public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {    if (_jsp_instancemanager == null) {      synchronized (this) {        if (_jsp_instancemanager == null) {          _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());        }      }    }    return _jsp_instancemanager;  }  public void _jspInit() {  }  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 java.lang.String _jspx_method = request.getMethod();    if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {      response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");      return;    }    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');      out.write('\n');      out.write('\r');      out.write('\n');      out.write("\r\n");      out.write("<html>\r\n");      out.write("<head>\r\n");      out.write("    <title>这是HTML头部</title>\r\n");      out.write("</head>");      out.write("\r\n");      out.write("<body>\r\n");      out.write("\r\n");      out.write("<div style=\"background-color: cadetblue\">\r\n");      out.write("    这是正文内容区域\r\n");      out.write("</div>\r\n");      out.write("\r\n");      out.write('\r');      out.write('\n');      out.write("\r\n");      out.write("<div style=\"background-color: antiquewhite;\">\r\n");      out.write("  这是footer底部内容区域\r\n");      out.write("</div>");      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 {            if (response.isCommitted()) {              out.flush();            } else {              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);    }  }}

通过上面源代码,可以看到,使用include指令引入的两个文件,最终都会将两个文件中的内容直接嵌入到当前include.jsp文件里面,如下所示:

所以include指令的本质就是将引入文件中的内容,直接拼接到当前JSP页面的对应位置。这里也就会存在一个问题,引入的JSP文件中,不能存在和当前JSP页面相同的变量名称,因为变量名称相同会导致编译失败。另外,使用include指令引入其他的JSP文件时候,只会生成访问的那个JSP文件的源代码,被引入的JSP文件不会生成对应的源代码。

以上,就是include指令的使用及其本质。

今天就到这里,未完待续~~

标签: #jsp中include #jsp中include file #jsp中include指令与include动作的区别