This article describes the method of using jsp:include to control dynamic content. Share it for your reference, as follows:
Listing 1. JSP include directive
<![CDATA[ <%@ page language="java" contentType="text/html" %> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="/styles/" rel="stylesheet" type="text/css" /> </head> <body> <%@ include file="" %> <%@ include file="" %> <%@ include file="" %> <%@ include file="/mt-blogs/" %> <%@ include file="" %> </body> </html> ]]>
Although include is ideal for incorporating static content into web pages, it is not satisfactory for dynamic content. We discovered this issue in the previous post when trying to reload the cached file. Unlike most header files and footer files, dynamic content changes frequently and must be updated at all times. We will first briefly recapitulate the limitations of the include directive, and then I will show you how to extend the JSP's inclusion capabilities with the jsp:include tag.
Caching issues
One disadvantage of the JSP include directive is that it causes the web browser to cache all pages. This makes sense when dealing with static components such as footers, copyright claims, or a set of static links. These files won't change, so there's no reason for the JSP interpreter to keep repolling the data in it. Wherever possible, caching should be implemented because it improves the performance of the application.
However, sometimes, caching will be less rewarding. If the content introduced comes from a program that uses dynamic data such as Weblog or database-driven JSP files, or even if the content contained is often changed HTML such as timestamps, then the latest version of these files or programs will be displayed whenever a web page is loaded. Unfortunately, the JSP include directive does not have this function. Disabling cache in a browser usually solves this problem during the test and development cycle (see sidebar "JSP Testing and Development". However, performance is an important factor in any design decision-making process for practical applications, and disabling cache is not a viable long-term solution. A better solution is to use the jsp:include tag.
jsp:include tag
jsp:include is just a pseudo directive that is different from include. The advantage of jsp:include is that it always checks for changes in the included files. We'll look at how this new marker works in a while. But first look at the codes of the two include each so that you can see the similarities and differences between the two.
Listing 2 shows a simple page using the original JSP include directive.
Listing 2. JSP include directive
<![CDATA[ <%@ page language="java" contentType="text/html" %> <html> <head> <title>JSP include element test</title> </head> <body> This content is statically in the main JSP file.<br /> <%@ include file="" %> </body> </html> ]]>
Listing 3 is the same page, but here it is converted to using the jsp:include tag.
Listing 3. Convert to using jsp:include
<![CDATA[ <%@ page language="java" contentType="text/html" %> <html> <head> <title>JSP include element test</title> </head> <body> This content is statically in the main JSP file.<br /> <jsp:include page="" flush="true" /> </body> </html> ]]>
You should be aware of the two major differences between these two code types. First, the jsp:include element does not use the %@ syntax belonging to the include directive. In fact, the jsp prefix lets the JSP compiler know: it should look for elements in the standard JSP tag set. Secondly, specify that the properties of the file to be included change from file to page . If you like, you can test the results of the new mark yourself. Simply change the contents of the file in the previous article (see Resources) and reload the browser page and you will see the new content immediately.
How does jsp:include work
If you are a little fond of digging, you may be very interested in why the jsp:include tag behaves differently from the include directive. The reason is actually very simple: jsp:include contains the response of the included URI, not the URI itself. This means: interpreting the pointed URI, thus including the generated response. If the page is HTML, you will get HTML without any change. However, if it is a Perl script, Java servlet, or CGI program, then the result will be explained from the program. Although pages are usually HTML, actual programs happen to be the means to achieve their goal. Moreover, since the page is interpreted every time you request the page, the results are never cached like when using the include directive. While this is only a small change, it causes all the differences in the behavior you see.
A mixed and match solution
include directives have their own use on some websites. For example, if the site contains some (and very few if there are changes) header, footer, and navigation files that are barely unchanged, the basic include directive is the best option for these components. Since the include directive uses cache, the contents will be cached only if you put the include file once, and the result will greatly improve the performance of the site.
However, for many web applications or sites today, carpet-style caching doesn't solve the problem. Although the header and footer may be static, it is impossible for the entire site to be static. For example, it is common to extract navigation links from databases, and many sites based on JSP technology also extract content from dynamic JSP pages on other sites or applications. If you are working on dynamic content, you need to use jsp:include to process the content.
Of course, the best solution is to frequently mix and match the two methods, using each construction where it is most appropriate. Listing 4 is an example of a mix-and-match inclusion solution.
Listing 4. Mixed and matched solutions
<![CDATA[ <%@ page language="java" contentType="text/html" %> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="/styles/" rel="stylesheet" type="text/css" /> </head> <body> <jsp:include page="" flush="true"> <jsp:param name="pageTitle" value=""/> <jsp:param name="pageSlogan" value=" " /></jsp:include> <%@ include file="/" %> <jsp:include page="" flush="true" /> <jsp:include page="/mt-blogs/" flush="true" /> <%@ include file="/" %> </body> </html> ]]>
The above code shows the example index page from the previous article. Navigation links and footers are static content that changes at most once a year. For these files, I used the include directive. The content pane contains the Weblog and "bookshelf" components, which are dynamically generated. These two components need to be updated all the time, so for them, I used the jsp:include tag. The file is a little weird. This component is extracted from another essentially static JSP page. However, as you will notice, it extracts the page "slogan" from the containing page and then displays it. To process this shared information, we must pass parameters to the header file. To process those parameters, the jsp:include element must be used.
I hope this article will be helpful to everyone's jsp programming.