前言

在jsp中,要引入一个外部文件可以通过两种方式:

I.通过jsp指令​​<%@include file="" %>​​​引入
II.通过jsp动作标签​​​<jsp:inclue page="">​​引入

其中第一种方式称为静态引入,第二种方式称为动态引入

静态引入

静态引入是指在把两个页面翻译成Servlet的过程中进行合并
下面是静态引入合并后的Servlet的代码

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(" ");
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"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=ISO-8859-1\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write('\r');
out.write('\n');
out.write("\r\n");
out.write("\t<!-- -->\r\n");
out.write("\t<h3>Simple2</h3>\r\n");
out.write("\t");
out.println(a);

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\" \"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=ISO-8859-1\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<!-- 动态引入 -->\r\n");
out.write("\t");
pageContext.setAttribute("name", "张三");
application.setAttribute("age", 12);

out.write("\r\n");
out.write("\t\r\n");
out.write("\t");

从合并后的Servlet的代码可以看出,静态引入完全是把一个页面的内容合并到了另一个页面(除了page指令)

动态引入

动态引入是指在Servlet的运行过程中,通过代码引入另一个页面

out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"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=ISO-8859-1\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<!-- 动态引入 -->\r\n");
out.write("\t");
//通过JspRuntimeLibrary的include方法引入jsp页面 org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "simple.jsp", out, false);
out.write('\r');
out.write('\n');
out.write(' ');
pageContext.setAttribute("name", "张三");
application.setAttribute("age", 12);

out.write("\r\n");