JSP Best Practices: Control Dynamic Content with JSP: Include

xiaoxiao2021-03-06  39

Writers, O'Reilly and Associates 2003 This is a follow-up article after the first JSP best practice article, in the article, the author demonstrates how to expand the dynamic content of how to extend JSP technology in the text. The containment function. Understand the difference between static include directives and dynamic JSP: Include elements, find out how to mix the two to obtain optimal performance. In the previous article of the new JSP Best Practice Series, you understand how to use the JSP Include directive to include static contents such as headers, footers, and navigation components to the web page. As with the server side, the JSP Include directive allows a page to extract content or data from another page. Listing 1 Remove the include directive. Listing 1. JSP include directive 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" /> </ head> <body> <% @ include file = "Header.jsp"%> <% @ include file = "navigation.jsp"%> <% @ include file = "bookshelf.jsp"%> <% @ include File = "/ mt-blogs / index.jsp"%> <% @ include file = "Footer.jsp"%> </ body> </ html>]]> What do you need all of this series All of the best practices JAVASERVER PAGES technology. To run any of the best practices, you need to install a Web container that meets JSP technology on your local machine or test server. You also need to use a text editor or IDE to encode JSP page. Although INCLUDE is very suitable to incorporate static content into the web page, it is not satisfactory for dynamic content. We disconnected this problem when trying to reload the cache file in the previous article. Unlike most header files and footer files, dynamic content changes frequent, and must be updated. We will first step on the limitations of the include directive, and then I will show you how to use JSP: Include tag to extend the inclusion of JSP. The cache problem JSP include the shortcomings of the directive: it will cause the web browser to cache all pages. This makes sense when dealing with static components such as footprints, copyright declarations, or a set of static links. These files do not change, so there is no reason to let JSP interpreters constantly poll the data. It is possible to achieve a cache because it improves the performance of the application. JSP testing and development When building a web application or website, you may need a lot of update headers, footer, and navigation links.</p> <p>This may be a painful thing that is forced to see the browser or clear its cache for the changes to the included files. On the other hand, in order to end the development cycle, it is also a painful thing to completely check the page and modify hundreds of pages that use the include directive. My suggestion is to disable the browser cache during the test. In most cases, doing this can completely solve problems. There is also a very small number of situations, do not work, then you can constantly restart the web container on your browser or server to ensure that the cache is not performed. However, sometimes, the cache will be lost. If the content raised is from the program that uses dynamic data (such as a WebLog or database-driven JSP file), even if the content contained is HTML (such as timestamp), then whenever a web page is loaded, Display the latest version of these files or programs. Unfortunately, the JSP Include directive does not have this feature. In the test and development cycle (see Side Bar "JSP Test and Development"), disable caches in your browser usually solve this problem. However, for applications actually used, performance is an important factor in any design decision process, and disabled caches is not a feasible long-term meter. Better solution is to use JSP: Include tags. JSP: Include Tag JSP: INCLUDE is just a direct order different from the include in Include. The advantage of JSP: Include is that it always checks the changes in the file included. After a while we will study this new marker work. But first take a look at the different code of the INCLUDE, so that you can see the difference between the two. Listing 2 shows a simple page that uses the original JSP include directive. Listing 2. JSP include directive <! [Cdata [<% @ page language = "java" contentty = "text / html"%> <html> <head> <title> JSP include element test </ title> </ head > <body> this content is statically in the main jsp file. <br> <% @ include file = "incruded.html"%> </ body> </ html>]]> Listing 3 is the same page, only However, it is converted to use the JSP: Include tag. Listing 3. Transfer to use JSP: include <! [Cdata [cdata "=" java "contentty =" 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 = "included.html" flush = "true" /> </ body> </ html>]]> you It should be noted that two differences between these two code types should be paid. First, the jsp: The include element does not use% @ grammar that belongs to the include directive.</p> <p>In fact, the JSP prefix allows the JSP compiler to know: it should look for elements in the standard JSP tag set. Second, specify the properties of the files to be included to become paged from File. If you prefer, you can test the results of the new tag yourself. Simply change the content of the incruded.html file in the previous article (see Resources), then reload the browser page, see new content immediately. FLUSH properties You may have noticed the flush attribute in the JSP: Include Code Example. As the name suggests, Flush indicates whether it is clear any existing buffer before reading the content. The flush attribute is required in JSP 1.1, so if you don't need it in your code, you will get an error. However, in JSP 1.2, the Flush property is default to false. Because it is not an important issue most of the time, my suggestion is: For JSP 1.1, set flush to true; for JSP 1.2 and later, it is set to shut down. JSP: INCLUDE is how to work if you have a little love to the roots, then you may want to know why the JSP: Include tag is different from the include directive. The truth is actually very simple: JSP: Include contains the response included in the included URI, not the URI itself. This means: explains the NRI pointed out, so that the generated response is included. If the page is HTML, it will get HTML that has not changed. However, if it is a Perl script, a Java Servlet, or a CGI program, the result will be the result of the program. Although the page is usually HTML, the actual program happens to achieve the purpose of achieving the purpose. Moreover, since each request page is interpreted, it will never be cached as the use of the include directive. Although this is only a small change, it has led to all the differences in the behavior you have seen. A mixed-matching solution include that there is a place in some websites on some websites. For example, if the site contains some (if there is a change, few) have almost no change, footer, and navigation files, then the basic include directive is the best option for these components. Since the include directive uses a cache, it is only necessary to put into the included file, and its content will be cached, and the result will greatly improve the performance of the site. However, for many web applications or sites, the carpet type cache does not solve the problem. Although the headers and footers may be static, it is not possible that the entire site is static. For example, extracting a navigation link from a database is common, and many JSP-based sites are also extracted from other sites or dynamic JSP pages on other sites or applications. If you are processing dynamic content, you need to use JSP: include to process the content. Of course, the best solution is to mix these two methods to use the use, and each structure is used to use the most appropriate place. Listing 4 is an example of a mixed match containing a solution.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-77254.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="77254" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.042</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'CRp2kKXggYgOfJ_2Bapqr3MxG49DJKJ8xbt6lf1AuFJNsqXa9MIlWZGy6gI61xRDmgKp4j2IRolN3igpq1_2BpCvOg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>