Are global variables in the included file (using <jsp:include/>) available in the file that includes it?

Luigi Viggiano

No: using <jsp:include /> you make a dynamic include, that will result in 2 different servlet to be generated: the outside one will call the included one and return its context in place of the <jsp:include/> tag.

Using <%@ include file="include.jsp" %> will make outside variables to be seen in the included page, as this will result in a compilation-time static include generating 1 single Servlet to accomplish JSP requests.

If you want to pass some variables from an outside page to an included one with a dynamic include you have to pass some parameters as shown following:

  String myvar = "hello";
  <jsp:include page="included.jsp" flush="true" >
    <jsp:param name="myvar" value="<%= myvar %>" />

Dynamic include will be obviously slower than statics, as they will be accomplished with one more request to the server made by the including page to display included content inside it.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact