JSP <c:import>和<jsp:include>区别【转】
<jsp:include page=""/> action和<c:import url=""/> jstl tag都是动态加载。但是c:import则更为灵活。jsp:include只能include当前web app下的页面,而c:import则可以从外部容器加载内容。
动态加载和静态加载最根本的不同在于,jsp生成的servlet的不同。在tomcat下静态(include directive)和动态(include action)的_jspService()代码如下:
- out.write(“<html><body>\r”);
- <span style="font-family: webdings;">out.write(“\r<html>\r<body>\r<img src=\”images/Web-Services.jpg\” >
- <br>\r<em><strong>We know how to make SOAP suck less.</strong></em> <br>\r\r
- </body>\r</html>\r”);</span>
- out.write(“\r<br>\r\r\r<em>We can help.</em> <br><br>\r\rContact us at: “);
- out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.
- proprietaryEvaluate(“${initParam.mainEmail}”, java.lang.String.class,
- (PageContext)_jspx_page_context, null, false));
- out.write(“\r\r\r</body></html>”);
而动态的则是:
- out.write(“<html><body>\r”);
- <span style="font-family: webdings;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response,
- “Header.jsp”, out, false);</span>
- out.write(“\r<br>\r\r\r<em>We can help.</em> <br><br>\r\rContact us at: “);
- out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.
- proprietaryEvaluate(“${initParam.mainEmail}”, java.lang.String.class,
- (PageContext)_jspx_page_context, null, false));
- out.write(“\r\r\r</body></html>”);
可以看到静态的是将要include的页面直接写在了servlet里面,这样当servlet生成之后,就不能修改了。动态的则不同,运行时才会导入页面。
另外jsp:include和c:import可以分别使用jsp:param和c:param来设置参数来控制要include的页面。