servlet中
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.include(request,response);
jsp中
<%@ include file="path" %> (目前我沒有測試出來這兩者的區別,可能是使用eclipse的原因,一旦保存了后,將自動構建)
<jsp:include page="<%=path %>" >
這里面的 path 如果以 /起頭,比如
/test/hello.jsp 就代表上下文的這個文件
./test/hello.jsp 當前文件下的/test/hello.jsp
../test/hello.jsp 當前文件的父親的/test/hello.jsp
path同樣可以是servlet里的mapping映射
servlet使用外部配置導入資源,需要在 web.xml進行配置
<servlet>
<description></description>
<display-name>HelloWorldServlet</display-name>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>testservlet.HelloWorldServlet</servlet-class>
<init-param>
<param-name>file</param-name>
<param-value>/PrintLog</param-value>
</init-param>
</servlet>
通過 String value = (String) getInitParameter("file"); 得到 /PrintLog
jsp使用外部配置導入資源
<%
java.util.ResourceBundle bundle = new java.util.ResourceBundle.getBundle("com.jspservletcookbook.include");
String segment = budle.getString("external-include");
%>
文件 include.properties 儲存在 WEB-INF/classes/com/jspservletcookbook
文件 include.properties內容為:external-include=WEB-INF/jspf/header_tag.jsp
jsp導入上下文之外的內容
使用 c:import JSTL核心標記