Introduction: This is a follow-up article from the Java Brett McLaughlin following the first JSP best practice article, in which the author shows you how to extend the inclusion functionality for dynamic content in JSP technology. Understand the differences between static include pseudo directives and dynamic jsp:include elements, figuring out how to mix and match them to get the best performance.
In the previous article in the new JSP best Practices series, you learned how to use JSP include pseudo directives to include static content such as headers, footers, and navigation components in a Web page. As with server-side inclusion, JSP include pseudo directives allow a page to extract content or data from another page. Listing 1 revisits the include pseudo directives.
Listing 1. JSP include pseudo instruction
<%@ page language="java" contentType="text/html" %>
<title>newInstance.com</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<link href="/styles/default.css"
rel="stylesheet" type="text/css" />
<body>
<%@ include file="header.jsp" %>
<%@ include file="navigation.jsp" %>
<%@ include file="bookshelf.jsp" %>
<%@ include file="/mt-blogs/index.jsp" %>
<%@ include file="footer.jsp" %>
</body>
Although include is ideal for incorporating static content into Web pages, it is not as satisfying as dynamic content. We found this problem in our previous article when we tried to reload the cache file. Unlike most header files and footer files, dynamic content changes frequently and must be updated at all times. We'll begin by briefly restating the limitations of the include pseudo directives, and then I'll show you how to extend the capabilities of the JSP with jsp:include tags.
Cache problem
One of the drawbacks of JSP include pseudo directives is that it causes the Web browser to cache all pages at cache. This is useful when working with static components such as footers, copyright notices, or a set of static links. These files do not change, so there is no reason for the JSP interpreter to repeatedly poll the data. Cache should be implemented wherever possible, as it improves the performance of the application.
Sometimes, however, caching is not worth the candle. If you are using a program that uses dynamic data, such as Weblog or database-driven JSP files, or even if the content is a frequently changing HTML (such as a timestamp), you will need to display the latest version of those files or programs whenever you load a Web page. Unfortunately, JSP include pseudo directives do not have this functionality. In the Test and development cycle (see Sidebar, "JSP Test and development"), disabling caching in a browser can often solve the problem. However, for applications that are actually used, performance is an important factor in any design decision process, and disabling caching is not a viable long-term measure. A better solution is to use the jsp:include tag.