IT Notes‎ > ‎Java‎ > ‎Java Web / Java EE‎ > ‎JavaServer Page‎ > ‎JSP语法详解‎ > ‎

What's the difference between @include and jsp:include?

简单地说:<@include>是静态包含,<jsp:include> 是动态包含。
<@include>,在编译之前,会把 JSP 片段内容嵌入进去。
<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 中有:
      org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "hello.html", out, false);

include2.jsp 中有:
      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",但最后中文还是有问题,原因不明,用过滤器设置也不行。

代码存放在:http://git.oschina.net/iridiumcao/iridiumonline/tree/master/helloweb_jsp_include
Comments