JSP programming best practice: Use an appropriate inclusion mechanism
JSP supports two inclusion mechanisms: Static include and Dynamic include.
- Static include
- The static include directive causes the content of the specified file to be textually inserted into the including file at compile time, that is, when the JSP page is translated into a Java servlet. JSP elements of the included file are thus effectively visible anywhere in the resulting JSP page and must have unique names to avoid name clashes. Typical Web containers are unable to detect changes made to the included file at run time, and hence the including file needs to be updated for the changes to take effect. The syntax for the static include directive is as follows
<%@ include file=" filename.jspf" %>- Dynamic include
- The dynamic include actions include the response generated by executing the specified page during the request processing phase, that is, when the including page is requested by the user. Since what is included is the response generated by the page, and not the content of the page itself, scripting variables declared in the included file are not visible elsewhere in the resulting JSP page. Changes made to the included file, on the other hand, are transparent to the including file. Two alternatives exist for the dynamic include:
- <c:import url=" filename.jsp"> ... </c:import>
- <jsp:include page=" filename.jsp" flush=" true|false"> ... </jsp:include>
Unlike jsp:include, the c:import action provides a mechanism to access resources that can be specified through a URL, thus allowing page authors to get access to resources that reside outside the Web application. On the other hand, it lacks the ability to flush the response. Finally, c:import is comparatively more heavyweight and is therefore not appropriate when a lightweight solution is sought.
If your WebSphere Commerce site uses Dynamic caching, flush the response. To do so:
- Precede and follow every c:import action with a <%out.flush();%> scriptlet.
- Set flush="true" in your jsp:include actions.
Whereas the dynamic include is more flexible than the static include, it falls short in terms of efficiency. As a consequence, use static includes whenever possible so as to avoid performance issues.
Related reference
JSP programming best practice: Use the .jspf extension for JSP segments
WebSphere Commerce JSP programming best practices