Two uses of incrude in JSP

xiaoxiao2021-03-06  76

Two uses of incrude in JSP

We all know that there are two forms in the JSP in JSP, namely <% @ include file = ""%> , the latter is an act element. Where will they be used? How to use and what differences? This should be a problem that many people will see. Let's take a look.

Usually when some parts of all pages in the application (such as title, footer and navigation bar) are the same, we can consider using include. In the case of <% @ include file = "" ", which is JSP's include instruction element read into the contents of the specified page. And integrate these contents and the original page. (This process is in the translation stage: that is, JSP is translated into a stage of servlet. Here, the translation phase is explained: We know that the JSP page cannot be sent to the browser, all JSP elements must First processed by the server. This is done by transiting the JSP page to servlet, then execute this servlet. The server requires a JSP container to handle the JSP page. JSP container usually implemented in the form of servlet, this servlet is configured, Processing all requests to the JSP page. JSP container is responsible for converting the JSP page into servlet (called JSP page to implement class -jsp page importation class), and compile this servlet. These two steps constitute a translation stage. We will Knowing: The JSP page is to add the actual content of the page specified by the include instruction element (that is, code segment) to the JSP page introduced into its JSP page, and convert it by the JSP container into servlet after synthesizing a file. You can see this Generate a temporary Class file and a Java file. The following example. The server is called Tomcat, the JSP file introduced into the page called Test.jsp.

The introduced page is called Date.jsp. This JSP file is stored in the JSP code for time. The current context root is Test // ====== Date.jsp source file ===== / / <% @ page language = "java" contenttype = "text / html; charSet = GB2312"%> <% java.util.date date = new java.util.date (); string date_cn = "; string datestr = ""; Date.getday ()) {case 0: DATE_CN = "Japan"; Break; Case 1: Date_CN = "One"; Break; Case 2: Date_Cn = "2"; Break; Case 3: Date_Cn = "Three"; Break; Case 4: Date_CN = "4"; Break; Case 5: Date_Cn = "5"; Break; Case 6: Date_CN = "Six"; Break;} datestr = (1900 Date.GetyEAR () "New Year" 1) "Month" Date.getdate () "Day (Week" Date_CN ")";%> Document.write ("<% = datestr) > "); // ====== The following is the source file of TEST.JSP =====================" Java "ContentType =" text / html CHARSET = GB2312 "%> include </> <jsp: include page =" limited "/> <% - @ include file = "Date.jsp"% -> // We are here to introduce Date.jsp with two different forms of INCLUDE. <HEAD> <body> <Table> <TR> <TD> About the JSP in Include Two kinds of usage. Please pay attention. </ td> </ tr> </ table> </ body> </ html></p> <p>In Test.jsp files, we only output a line of text "about two uses of INCLUDE in JSP. Please pay attention.", Now let us use <% @ include file = "DATE.JSP"%> this form Introduced this file. What problems do you want to do? An error message appears: http status 500 -org.apache.jasper.jaspexception: /date.jsp (0, 0) Page Directive: Can't Have Multiple Occurrence Of ContentType There is still a bunch of errors, but we only look here Know the problem. The status code is an HTTP 500 server internal error. Look at the following tips. Multiple contentType cannot be specified in the Date.jsp page. The reason is here. It is because the code of the Date.jsp file is added to the Test.jsp page to synthesize a file. The same file after the synthesis: <% @ page language = "java" contentty = "text / html; charset = GB2312"%></p> <p>This code. The solution is to delete this sentence in the Date.jsp file. After refresh, ask the test.jsp page request TEST.JSP to display the page as follows</p> <p>December 10, 2003 13:12:40 Two kinds of use of include Include in JSP. Please pay attention.</p> <p>At this time, we can't find anything. Or go to see the temporary files under Tomcat. Go there to see if the content of the date.jsp file has been added to the Test.jsp file. <Note. The Tomcat here is in the root directory of the E disk></p> <p>Directory E: / Tomcat / Work / Standalone / LocalHost / Test.</p> <p>Two files of Test_jsp.java and Test_jsp.class are seen in this directory.</p> <p>The Java file here is the Test_Jsp.java file that the JSP container transforms JSP into servlets. The corresponding Test_jsp.class This file is the class file generated by compiling Test_Jsp.Java this servlet file. Open the generated servlet file (TEST_Jsp.Java). At this point we will find,</p> <p>When the test.jsp file is converted to a servlet file, some of the code inside the Test.jsp page is added, and the newly added content is the code inside DATE.JSP: What is added or Whether I really joined the new content, please test it, it will be clear. It is no longer detailed here. The above is the result of this form of this form.</p> <p>Below we are converted to <JSP: include page = "dae.jsp" flush = "true" /></p> <p><% @ include file = "DATE.JSP"%> Replaced <JSP: include page = "dae.jsp" flush = "true" />, then request Test.jsp.</p> <p>2003? Ê 12 ?? 10 ?? 13:30:13 About the two uses of Include in JSP. Please pay attention.</p> <p>At this time, you will see it on the page. There is a garbled in Chinese on the date that we introduce the date.jsp output. What reason is because the include behavior element is executed during the request processing phase. (Here you want to explain the request processing phase) The .jsp container is responsible for calling the JSP page implementation class in addition to the JSP page to serve the JSP page to handle each request and respond. This stage is called the request processing phase. Request processing Phase only So when we introduce the page in our incocre behavior, it is actually just referenced by the servlet class file that is converted and generated after being compiled. So, Date.jsp is actually executed as a separate file. Call is run when the Test.jsp file is run. Since there is no specified character encoding in the Date.jsp file. So there is a garbled. The solution is to re-remove the <% @ page language = "java" contentType = "Java" in the date.jsp file. "Text / html; charSet = GB2312"%></p> <p>This row statement is added to refresh again. At this point, the page is correct and followed by the include instruction. The temporary file under Tomcat will find it. At this time, there is more Date_Jsp.class Document. These two files are just like Test_Jsp.java and Test_jsp.class files. A click on the code of the test_jsp.java file will be found. At this time, only one code is added: JSpruntimelibrary. Include (Request, Response, "Date.jsp", OUT, TRUE;</p> <p>It did not add the code of the date.jsp file to Test.jsp. Just the response generated after the Date.jsp page is introduced, which means that we can specify any web resources capable of generating a response, (for example, one Servlet or a JSP page), as long as these resources generated, the same type generated by the JSP page. The JSP container will perform the specified resource through an internal function call. Therefore, these incoming resources can help process the original Request, so these resources can access all objects in the request scope. And all the original request parameters.</p> <p>Since when the home page is requested, these pages have not been introduced into the main page, so you can use a request to use a request value for the Page property to decide which page to introduce according to the situation at runtime. You can also add some Request parameters read by the page introduced.</p> <p><JSP: Include Page = "<JSELECTEDATRUNTIME%>" Flush = "true"> <jsp: param name = "fitstparamer" value = "firstvalue"> <jsp: param name = "lastparamer" value = "lastvalue"> </ jsp: incrude></p> <p>If you modify the introduced JSP page, you can use the latest version of this page immediately because the way to be introduced is exactly the same as the JSP page that is directly called by the browser. That is, the container detection page changes And automatically enter the translation phase to get the latest version of the page.</p> <p>(Note that the incrude behavior element is the same as the other elements of JSP. There is no behavior when there is no behavior. Just like this. <JSP: incrude Page = "<JSP: Include Page =" <JSELECTEDATRUNTIME%> "Flush =" true "/>)</p> <p>The following is the difference between the two usage of include</p> <p>There are two main two aspects;</p> <p>One: Execution time:</p> <p><% @ include file = "relativeuri"%> is executed in the translation phase <jsp: include page = "relativeuri" flush = "true" /> executes in the request processing phase.</p> <p>Second: The introduction of content is different:</p> <p><% @ include file = "relativeuri"%> Introduces Static Text (HTML, JSP), and merge it together before the JSP page is converted to servlet. <jsp: include page = "relativeuri" flush = "true" /> Introduce the response text generated by the execution page or servlet.</p> <p>In addition, in two methods, the file and page properties are interpreted as a relative URI. If it starts with a slash, then it</p> <p>It is an environment-related path. It will be interpreted according to the prefix to assign the application's URI. If it does not start with a slash, then the page related path is explained according to the path to the page of this file. More How is the URL explains, please refer to the relevant information or books.</p> <p>This is only some of the individual's understanding. Limited level, please advise. And hope to communicate with you, progress together, hope to make more suggestions.</p> <p>QQ: 262995777</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-93193.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="93193" 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.040</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 = 'MiJALQFATvLwh2eRRYjmtODPrhwZlBTxO5GOkRRyyjhanY2R6r7tmhf4Q1WxrPoEYsCjxqCOLfBANnSfVUuxSg_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>