If i declare a variable in my A.jsp and i am trying to include A.jsp to B.jsp.So my question stands here whether the variable declared in A.jsp is acess开发者_如何学运维able in B.jsp? Please explain me for both the cases Dynamic include and static include.
When you include a jsp template using <%@page include=""> the source will actually be inserted and compiled into the including file. This is what makes you able to use variables declared in the parent file.
When doing a "dynamic" include it will use RequestDispatcher.include which will invoke the calling page as a new Servlet. This makes you unable to use declared variables.
I would recommend you to pass variables on the request scope using request.setAttribute("name", obj); when doing this
You can't pass server-side parameters using the <%@ include %>
directive. This directive does a static include. The contents of the included file are statically inserted into the including page. That is, during translation time from jsp to servlet.
Use the <jsp:include>
tag instead, it is processed at runtime, and with it you can pass parameters using <jsp:param>
.
For instance, if you have a.jsp with
<jsp:include page="b.jsp" />
<jsp:param name="param1" value="value1" />
<jsp:param name="param2" value="value2" />
</jsp:include>
You can get those parameters as request parameters in b.jsp
<% String v = request.getParameter("param1"); %>
Take into account you can still get request parameters available on a.jsp in b.jsp.
精彩评论