在JSP中,有兩種 include
<%@include file="includes/header.jsp" %>
What you’re doing is a static include. A static include is resolved at compile time, and may thus not use a parameter value, which is only known at execution time.
What you need is a dynamic include:
<jsp:include page="..." />
Note that you should use the JSP EL rather than scriptlets. It also seems that you’re implementing a central controller with index.jsp. You should use a servlet to do that instead, and dispatch to the appropriate JSP from this servlet. Or better, use an existing MVC framework like Stripes or Spring MVC.
- 第一種, 沒有 page= 的include會在編譯時期(轉換成servlet)就將file include進來,最後只會有一個.class檔案.
- 第二種, 有 page= 的include, 在編譯時期並不會被編譯,是在client request時,才會動態的去載入在去編譯。
如果要帶參數, You can use parameters like that
<jsp:include page='about.jsp'>
<jsp:param name="articleId" value=""/>
</jsp:include>
and
in about.jsp you can take the paramter
<%String leftAds = request.getParameter("articleId");%>
You can use Include Directives
<%
if(request.getParameter("p")!=null)
{
String p = request.getParameter("p");
%>
<%@include file="<%="includes/" + p +".jsp"%>"%>
<%
}
%>
or JSP Include Action
<%
if(request.getParameter("p")!=null)
{
String p = request.getParameter("p");
%>
<jsp:include page="<%="includes/"+p+".jsp"%>"/>
<%
}
%>
資料來源:
https://stackoverflow.com/questions/9110148/include-another-jsp-file