龙空技术网

《JSP》第10节:JSP动作标签之文件引入

Java新手笔记 444

前言:

今天兄弟们对“tomcat直接访问html”可能比较关注,同学们都需要学习一些“tomcat直接访问html”的相关文章。那么小编同时在网上网罗了一些有关“tomcat直接访问html””的相关内容,希望看官们能喜欢,同学们快快来学习一下吧!

前面一个小节内容介绍了include指令,其中说到了一点,include指令引入其他文件的时候,不能存在相同名称的变量,如果存在相同名称的变量,访问时候就会报错,这是因为include指令是直接嵌入引入文件的内容到当前JSP页面里面,同一个页面中,不能存在两个相同的变量名称。

为了解决上面的问题,jsp提供了一个<jsp:include>标签,该标签的作用和include指令作用是相同的,都是引入其他的文件。两者的区别在于:include指令是静态引入,而<jsp:include>标签是动态引入。

include指令在文件发生变动之后,需要重新编译JSP文件生成Servlet程序,并且它是直接将引入的文件内容嵌入到当前JSP页面里面;而<jsp:include>标签是动态引入,如果引入的文件发生了变动,当前JSP文件不需要重新编译,因为动态引入过程中,每次都将重新访问引入的文件。

1.1、基础语法

<jsp:include>动作的语法规则如下所示:

<jsp:include page="文件的相对路径"/>

案例代码:

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

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

1.2、<jsp:include>的本质

<jsp:include>引入其他的文件,访问当前JSP页面的时候,此时容器会将当前JSP页面中使用了<jsp:include>标签的所有jsp文件进行编译,生成对应的Servlet程序,如下所示:

这也是和include指令的区别之一。接着看下include.jsp文件的源代码,如下:

/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/8.5.98 * Generated at: 2024-02-25 07:03:42 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;  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');      org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "header.jsp", out, false);      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');      org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "footer.jsp", out, false);      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);    }  }}

通过上面的源代码可以看出,<jsp:include>引入其他文件不是简单的将文件内容嵌入到当前页面,而是通过org.apache.jasper.runtime.JspRuntimeLibrary.include()方法,在程序访问的过程中,动态去引入需要的文件。

这种动态引入的优点是,引入的JSP文件中的变量和当前JSP文件中的变量是不冲突的,变量名称可以相同,也可以不同,都不影响程序的编译

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

标签: #tomcat直接访问html #jsp中include file #jsp输出标签 #html页面引入jsp