1 前言
JSP页面有两种方式引入外部文件,一种是include指令,另一种是include动作标签,现在来看看两者的具体使用和区别。
2 include指令
include指令语法为:<%@ include file=”child.jsp” %>,其中file内容为要引入文件路径,可为绝对路径或相对路径。
include指令也称为静态引入,为什么这么说,直接看代码。
①child.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>
我是子页面,要被引入的JSP页面.<br/>
<%
String s = "测试变量的使用";
%>
</body>
</html>
②main.jsp页面内容,即要引入其他文件的那个JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="child.jsp" %>
<!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指令引入子页面child.jsp.
<%
// 因为child.jsp已经定义了s变量,所以这里不能重新定义,只能直接使用变量s,否则报错
s = "我是主页面";
%>
</body>
</html>
③翻译后的java代码:静态引入只会生成一个java文件
-- 此处将静态引入的JSP放在一个HashMap中
static {
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1);
_jspx_dependants.put("/jsp/child.jsp", Long.valueOf(1489987321091L));
}
-- 通过out写出内容,先写出child.jsp文件内容,注意变量的定义
out.write('\r');
out.write('\n');
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \r\n"); out.write("\t\"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("\t我是子页面,要被引入的JSP页面.<br/>\r\n");
out.write("\t");
String s = "测试变量的使用";
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \r\n");
out.write("\t\"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("\t通过include指令引入子页面child.jsp.\r\n");
// 因为child.jsp已经定义了s变量,所以这里不能重新定义,只能直接使用变量s,否则报错
s = "我是主页面";
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
④由此可见,include指令是将引入文件的内容整合到一个Servlet中,相当于完全将内容复制,所以称为“静态引入”。
3 include动作
include动作标签语法为:<jsp:include page="child.jsp" />
include动作标签也被称为“动态引入”,具体看代码
①同include指令的child.jsp内容
②main.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动作引入子页面child.jsp.
<jsp:include page="child.jsp" />
<%
// 因为child.jsp和main.jsp会各自生成java文件,所以必须定义变量s,否则报错
String s = "我是主页面";
%>
</body>
</html>
③翻译后的java代码
翻译是分别生成了两个java文件。
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \r\n");
out.write("\t\"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("\t通过include动作引入子页面child.jsp.\r\n");
out.write("\t");
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "child.jsp", out, false);
out.write('\r');
out.write('\n');
out.write(' ');
// 因为child.jsp和main.jsp会各自生成java文件,所以必须定义变量s,否则报错
String s = "我是主页面";
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
可以看出,并没有将内容复制过来,而是通过
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, “child.jsp”, out, false);方式引入。
4 关联与区别
①两者都可以引入外部文件;
②include指令:主要优点是功能强大,所包含的代码可以含有总体上影响主页面的JSP构造,比如属性、方法的定义和文档类型的设定。它的缺点是难于维护只要被包含的页面发生更改,就得更改主页面,这是因为主页面不会自动地查看被包含的页面是否发生更改。主要用于引入页面不常改动的地方,如页面标题和版权页面。
③include动作标签:优点是在被包含的页面发生更改时,无须对主页面做出修改。它的缺点是所包含的是次级页面的输出,而非次级页面的实际代码,所以在被包含的页面中不能使用任何有可能在整体上影响主页面的JSP构造。几乎所有情况都可使用,一般建议使用include动作标签。