What's the difference between @include and jsp:include?
简单地说:<@include>是静态包含,<jsp:include> 是动态包含。
- <@include>,静态包含。在编译之前,会把 JSP 片段内容嵌入进去,编译成一个 servlet。
- <jsp:include>,动态包含。两段 JSP 代码分别编译,但包含的页面持有被包含页面的引用,被包含的页面的编译指令不会对包含页面造成影响。可以带参数,本文示例不涉及参数。
附上2009-10-18的一个小实验,如以下两个页面:
include1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="hello.html" />
和 include2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="hello.html"%>
它们包含的 hello.html 内容如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>reusable</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
Hello!<br/>
Ni hao!<br/>
你好!
</body>
编译後,include1.jsp 对应的 servelt 中有:
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "hello.html", out, false);
include2.jsp 对应的 servlet 中有:
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\r\n");
out.write(" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write(" <title>reusable</title>\r\n");
out.write(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("Hello!<br/>\r\n");
out.write("Ni hao!<br/>\r\n");
out.write("ä½ å¥½!\r\n");
out.write("</body>");
以上实验凡涉及到可设置编码的地方, 均为"UTF-8",但最後中文还是有问题,原因不明,用过滤器设置也不行。
代码存放在:https://github.com/iridiumcao/iridiumonline/tree/master/helloweb_jsp_include