在JSP中,可以使用include指令来包含其他jsp文件。例如,一个网站中,多个网页具有很大部分是相同元素时,可以使用include指令来完成。include的指令的语法如下:
<%@ include file="文件路径"%>
下面通过一个简单的实例来说明include指令实现的原理。使用Eclipse在web项目下新建一个jsp文件命名为header.jsp,具体代码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML> <html> <head> <title>My JSP 'header.jsp' starting page</title> </head> <body> 这是一个头文件 </body> </html>
然后新建一个jsp文件命名为include.jsp,具体代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML> <html> <head> <title>My JSP '03.include.jsp' starting page</title> </head> <body> <%@ include file="header.jsp" %> 这是一个主体部分 </body> </html>
使用include指令时,在file属性中指定要包含的jsp文件的路径,推荐使用相对路径。在eclipse中,按住Ctrl键,并点击file属性指定的路径能够打开相对应的jsp文件,说明路径正确。
将web项目部署到Tomcat服务器并启动服务器,到work目录中找到jsp文件翻译成的java文件。代码如下:
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.81 * Generated at: 2017-10-15 11:44:30 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 org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.util.*; import java.util.*; public final class _include_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("/common/header.jsp", Long.valueOf(1508067769708L)); } 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 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 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("\r\n"); out.write("<!DOCTYPE HTML>\r\n"); out.write("<html>\r\n"); out.write(" <head>\r\n"); out.write(" \r\n"); out.write(" <title>My JSP '03.include.jsp' starting page</title>\r\n"); out.write(" \r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" \t"); out.write("\r\n"); out.write("\r\n"); out.write("<!DOCTYPE HTML>\r\n"); out.write("<html>\r\n"); out.write(" <head>\r\n"); out.write(" \r\n"); out.write(" <title>My JSP 'header.jsp' starting page</title>\r\n"); out.write(" \r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" 这是一个头文件\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n"); out.write("\r\n"); out.write(" \t这是一个主体部分\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n"); } 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); } } }
可以看出,只翻译成一个java文件,jsp引擎在处理include指令时,只是单纯的将被包含文件的内容copy到包含文件中。查看out.write()代码部分,发现两个其中有html标签有重复的。因此,我们可以在header.jsp中去掉部分重复的标签,这样include.jsp也能正常的翻译成servlet程序。