The example in this article describes the method of using Jsp:include to control dynamic content. Share to everyone for your reference, specific as follows:
Listing 1. JSP include pseudo instruction
<! [cdata[
<%@ page language= "java" contenttype= "text/html"%>
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.
Jsp:include Mark
Jsp:include is nothing more than a pseudo instruction that is different from include. The advantage of jsp:include is that it always checks for changes in the contained files. In a moment we will look at how this new marker works. But first take a look at the two kinds of code, so that you can see the similarities and differences.
Listing 2 shows a simple page that uses the original JSP include pseudo directives.
Listing 2. JSP include pseudo instruction
<! [cdata[
<%@ page language= "java" contenttype= "text/html"%>
Listing 3 is the same page, except that it's turned into using the jsp:include tag.
Listing 3. Turn to use Jsp:include
<! [cdata[
<%@ page language= "java" contenttype= "text/html"%>
You should be aware of the two major differences between the two types of code. First, the Jsp:include element does not use the%@ syntax that belongs to the include pseudo directive. In fact, the JSP prefix lets the JSP compiler know that it should look for elements in a standard JSP tag set. Second, you specify the properties of the file you want to include from file into page. If you want, you can test the results of the new tag yourself. Just change the contents of the included.html file in the previous article (see Resources) and reload the browser page to see the new content immediately.
How Jsp:include is working.
If you're a bit inquisitive, you might be wondering why the jsp:include tag behaves differently from the include pseudo directive. The truth is very simple: Jsp:include contains the response of the included URI, not the URI itself. This means that the indicated URI is interpreted so that it contains the generated response. If the page is HTML, you'll get HTML that doesn't change at all. However, if it is a Perl script, a Java servlet, or a CGI program, you will get the result that is interpreted from the program. Although the page is usually HTML, the actual program is just the way to achieve it. Also, because it is interpreted every time the page is requested, the results are never cached as if the include pseudo directives were used. Although this is a small change, it causes all the differences in the behavior you see.
A mix-and-match solution
Include pseudo directives are useful on some sites. For example, if a site contains some headers, footers, and navigation files that have little or no change, the basic include pseudo directives are the best options for these components. Because the include pseudo directives are cached, the content is cached only once, and the result is a significant increase in the performance of the site.
However, a carpet cache does not solve the problem for many WEB applications or sites today. Although headers and footers may be static, it is not possible for the entire site to be static. For example, it is common to extract navigation links from a database, and many JSP technology-based sites also extract content from dynamic JSP pages on other sites or applications. If you are working with dynamic content, you need to use Jsp:include to process the content.
The best solution, of course, is to mix the two methods often, using each construct in the most appropriate place. Listing 4 is an example of a mix-and-match containing solution.
Listing 4. Mix and Match Solutions
<![ cdata[<%@ page language= "java" contenttype= "text/html"%>
The above code shows the sample index page in the previous article. Navigation links and footers are static content that is changed at most once a year. For these files, I use the include pseudo directives. The content pane contains Weblog and "bookshelf" components, which are dynamically generated. These two components need to be updated all the time, so for them, I use the jsp:include tag. header.jsp file is a bit strange. This component is extracted from another, essentially static, JSP page. However, as you will notice, it extracts the page "banner" from the containing page and then displays it. To handle this shared information, we must pass parameters to the header file. To handle those parameters, you must use the Jsp:include element.
I hope this article will help you with JSP program design.